Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'handler': 0.05; 'socket': 0.07; 'cherrypy': 0.09; 'connect,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'wrote': 0.14; 'thread': 0.14; 'changes': 0.15; 'all?': 0.16; 'blocks': 0.16; 'closed.': 0.16; 'frees': 0.16; 'loop.': 0.16; 'lot!': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'skipped': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'trying': 0.19; 'basically': 0.19; 'passing': 0.19; 'result.': 0.19; 'server,': 0.19; 'thu,': 0.19; 'solution.': 0.20; 'seems': 0.21; 'programming': 0.22; 'handling': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'requests': 0.31; "skip:' 10": 0.31; 'that.': 0.31; 'checked': 0.32; 'open': 0.33; 'becomes': 0.33; 'connection': 0.35; 'except': 0.35; 'point.': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'version': 0.36; 'so,': 0.37; 'server': 0.38; 'process,': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ensure': 0.60; 'worry': 0.60; 'new': 0.61; 'browser': 0.61; 'effective': 0.61; 'entire': 0.61; 'first': 0.61; 'back': 0.62; 'soon': 0.63; 'refer': 0.63; 'more': 0.64; 'effectively': 0.66; 'determine': 0.67; 'frank': 0.68; 'request.': 0.70; 'food': 0.72; 'connection.': 0.74; 'listening': 0.74; 'reply,': 0.93; 'connection,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: threading Date: Thu, 10 Apr 2014 13:10:54 +0200 References: <87d2gt4td2.fsf@elektro.pacujo.net> <7xha651yx2.fsf@ruckus.brouhaha.com> <877g70wg8p.fsf@elektro.pacujo.net> X-Gmane-NNTP-Posting-Host: 197.87.184.88 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397128288 news.xs4all.nl 2895 [2001:888:2000:d::a6]:50920 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70029 "Chris Angelico" wrote in message news:CAPTjJmoWaHPZk=DAxbfJ=9ez2aj=4yf2C8WMbRYoF5VgN6Exsw@mail.gmail.com... > On Thu, Apr 10, 2014 at 7:17 PM, Frank Millman wrote: >> The current version of my program uses HTTP. As I understand it, a client >> makes a connection and submits a request. The server processes the >> request >> and returns a result. The connection is then closed. >> >> In this scenario, does async apply at all? There is no open connection to >> 'select' or 'poll'. You have to ensure that the request handler does not >> block the entire process, so that the main loop is ready to accept more >> connections. But passing the request to a thread for handling seems an >> effective solution. > [...] Thanks, Chris - I am learning a lot! I have skipped the first part of your reply, as it seems to refer to the client. I am using a web browser as a client, so I don't have to worry about programming that. > > When you write the server, you effectively have the same principle, > with one additional feature: a listening socket becomes readable > whenever someone connects. So you can select() on that socket, just > like you can with the others, and whenever there's a new connection, > you add it to the collection and listen for requests on all of them. > It's basically the same concept; as soon as you can accept a new > connection, you do so, and then go back to the main loop. > This is where it gets interesting. At present I am using cherrypy as a server, and I have not checked its internals. However, in the past I have dabbled with writing server programs like this - while self.running: try: conn,addr = self.s.accept() Session(args=(self, conn)).start() except KeyboardInterrupt: self.shutdown() In this scenario, the loop blocks on 'accept'. You seem to be suggesting that I set the socket to 'non-blocking', use select() to determine when a client is trying to connect, and then call 'accept' on it to create a new connection. If so, I understand your point. The main loop changes from 'blocking' to 'non-blocking', which frees it up to perform all kinds of other tasks as well. It is no longer just a 'web server', but becomes an 'all-purpose server'. Much food for thought! Frank