Re: [Tutor] Infinite Loops (and threads)
Luke Paireepinart wrote: > Kent et. al., > > I'm writing something that has to do with sockets. > I need to recv any incoming packets from the socket. > I will have potentially hundreds of separate sockets open in a single > application. > I was just going to create a thread for each, so I could receive from > them separately. Yes, that is one way to do it - make a thread for each socket and use blocking I/O. Then each thread will block until data is available. This avoids the kind of busy wait loop the OP described. > > Alternately, I figured I could read from each socket in sequence if I > could get the recv method to not block until it gets input, That is another good approach. You might want to look at Twisted or Medusa, they are both server frameworks that use asynchronous, non-blocking I/O to manage multiple sockets in a single thread: http://twistedmatrix.com/trac/ http://www.nightmare.com/medusa/ > Do I misunderstand what blocking is? > It seems to me that blocking would mainly apply to inputs. Usually, though output can block too. > (My understanding is that 'blocking' means when you call 'recv' it will > return '' if it didn't receive anything.) No; with a blocking socket, recv() will not return until data is available; with a non-blocking socket recv() will raise an exception if data is not available. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Infinite Loops (and threads)
Luke Paireepinart <[EMAIL PROTECTED]> On Sun, 24 Dec 2006 10:02:19 -0600 wrote: > Kent Johnson wrote: > Kent et. al., > > I'm writing something that has to do with sockets. > I need to recv any incoming packets from the socket. > I will have potentially hundreds of separate sockets open in a single > application. > I was just going to create a thread for each, so I could receive from > them separately. > > Alternately, I figured I could read from each socket in sequence if I > could get the recv method to not block until it gets input, > so I tried this, > > Thanks, > -Luke Use "import select" and read about it for more information (i.e. help(select) within Python shell). I advice you (and others interested in writing Python code dealing with sockets) *very strongly* to read the following: http://www.amk.ca/python/howto/sockets/ (Currently, I'm having trouble accessing that URL. I don't know if it's my ISP or the the site is down.) I hope that helps. Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Infinite Loops (and threads)
On 24/12/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: Kent Johnson wrote: > Jonathan McManus wrote: > >> Hi all, >> >> Just a quick question, really. Is there any good way to have an infinite >> loop in a program, without said infinite loop eating up the CPU? I've >> tried the trick of adding a pause (time.sleep(0.01)) somewhere in the >> loop, and this appears to have worked on a basic infinite loop, but this >> doesn't appear to work for two separate infinite loops (in threads). >> > > You would have to put a sleep in each thread. > > Why are you using infinite loops? Are you implementing some kind of > polling loop? Often there are better alternatives, either an event > notification or some kind of lock. If you post some details of why you > want to do this we may be able to help you find a better way. > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Kent et. al., I'm writing something that has to do with sockets. I need to recv any incoming packets from the socket. I will have potentially hundreds of separate sockets open in a single application. I was just going to create a thread for each, so I could receive from them separately. Alternately, I figured I could read from each socket in sequence if I could get the recv method to not block until it gets input, so I tried this, #code self.conn.setblocking(False) info = self.conn.recv(8000)#this should read everything. # code where conn is connected to another computer already, and I get the following error: (Verbatim except for line 3, changed to hide username/password) Traceback (most recent call last): File "C:\Python Scripts\AIM Connection Server\toc2.py", line 199, in ? toc.login('---username---','---password---') File "C:\Python Scripts\AIM Connection Server\toc2.py", line 116, in login print self.getFlap() File "C:\Python Scripts\AIM Connection Server\toc2.py", line 93, in getFlap info = self.conn.recv(8000)#this should read everything. error: (10035, 'The socket operation could not complete without blocking') Do I misunderstand what blocking is? It seems to me that blocking would mainly apply to inputs. (My understanding is that 'blocking' means when you call 'recv' it will return '' if it didn't receive anything.) I'd appreciate any links that I could read up on, or any advice on how to make socket inputs with event notification, as Kent mentioned earlier. Basically, as Kent said, I have a polling loop, and I am not sure what the alternative is to threads. Thanks, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well I've been doing some networking stuff in stackless myself recently and I would probably do something like this: import select import stackless import socket def get_data(sock): sock.setblocking(0) poller = select.poll() poller.register(sock, select.POLLIN) while True: if poller.poll(0): sock.recv(1024) stackless.schedule() stackless.tasklet(get_data)(socket.socket(socket.AF_INET, socket.SOCK_DGRAM )) stackless.run() stackless thread things are really low on resources so you can run tens of thousands without too much trouble. HTH, Adam. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Infinite Loops (and threads)
Kent Johnson wrote: > Jonathan McManus wrote: > >> Hi all, >> >> Just a quick question, really. Is there any good way to have an infinite >> loop in a program, without said infinite loop eating up the CPU? I've >> tried the trick of adding a pause (time.sleep(0.01)) somewhere in the >> loop, and this appears to have worked on a basic infinite loop, but this >> doesn't appear to work for two separate infinite loops (in threads). >> > > You would have to put a sleep in each thread. > > Why are you using infinite loops? Are you implementing some kind of > polling loop? Often there are better alternatives, either an event > notification or some kind of lock. If you post some details of why you > want to do this we may be able to help you find a better way. > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Kent et. al., I'm writing something that has to do with sockets. I need to recv any incoming packets from the socket. I will have potentially hundreds of separate sockets open in a single application. I was just going to create a thread for each, so I could receive from them separately. Alternately, I figured I could read from each socket in sequence if I could get the recv method to not block until it gets input, so I tried this, #code self.conn.setblocking(False) info = self.conn.recv(8000)#this should read everything. # code where conn is connected to another computer already, and I get the following error: (Verbatim except for line 3, changed to hide username/password) Traceback (most recent call last): File "C:\Python Scripts\AIM Connection Server\toc2.py", line 199, in ? toc.login('---username---','---password---') File "C:\Python Scripts\AIM Connection Server\toc2.py", line 116, in login print self.getFlap() File "C:\Python Scripts\AIM Connection Server\toc2.py", line 93, in getFlap info = self.conn.recv(8000)#this should read everything. error: (10035, 'The socket operation could not complete without blocking') Do I misunderstand what blocking is? It seems to me that blocking would mainly apply to inputs. (My understanding is that 'blocking' means when you call 'recv' it will return '' if it didn't receive anything.) I'd appreciate any links that I could read up on, or any advice on how to make socket inputs with event notification, as Kent mentioned earlier. Basically, as Kent said, I have a polling loop, and I am not sure what the alternative is to threads. Thanks, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Infinite Loops (and threads)
Jonathan McManus wrote: > Hi all, > > Just a quick question, really. Is there any good way to have an infinite > loop in a program, without said infinite loop eating up the CPU? I've > tried the trick of adding a pause (time.sleep(0.01)) somewhere in the > loop, and this appears to have worked on a basic infinite loop, but this > doesn't appear to work for two separate infinite loops (in threads). You would have to put a sleep in each thread. Why are you using infinite loops? Are you implementing some kind of polling loop? Often there are better alternatives, either an event notification or some kind of lock. If you post some details of why you want to do this we may be able to help you find a better way. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor