[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] using urllib



You would use a Queue.Queue to communicate from the mainloop to the worker thread, but the worker thread should dispatch an event back to the mainloop. You can put the result right into the Event object. Otherwise, you would have to poll the Queue from your main loop to see if there's any data on it yet. Posting an event is far better as it allows you to wake up and process it as soon as it's ready.

It is perfectly thread safe to post an event from an alternate thread, but if you're using pygame.event then you'll have to watch out because posting an event MIGHT raise an exception. In pygame CVS, there is an alternative module for doing events, pygame.fastevent, which is much higher performance in the face of threads and will not raise any exceptions in weird places when the queue is full (it will simply wait on a mutex until it's no longer full).

An example of using the event system (either the old one or the new one) for thread communication is here:
http://svn.twistedmatrix.com/cvs/*checkout*/trunk/doc/core/examples/ threadedselect/pygamedemo.py?root=Twisted


Alternatively, you could use Twisted's twisted.web.client.getPage() instead of urllib... but I probably wouldn't go that far if all you want to do is fetch a few URLs sporadically.

-bob


On Apr 19, 2005, at 5:28 PM, Shandy Brown wrote:

Thanks Sami.

I also took a look at SolarWolf, which has some urllib code to fetch
"News" in gamenews.py. Looks like it writes the info to a file and lets
the OS deal with the concurrency issues.


I think Queue will be more suited to my needs.

-sjbrown

On Tue, 19 Apr 2005, Sami Hangaslammi wrote:

On 4/19/05, Shandy Brown <sjbrown@xxxxxxxxx> wrote:
can anyone recommend any strategies for using urllib for doing simple
pulling of information with pygame?

the urlopen and urlretrieve functions block, which is not ideal for
pygame apps.  how have people dealt with this, by starting a Thread?

If a Thread was used, how has the info been put back into the pygame
mainloop? Can you put an event on the pygame event queue from the new
thread?


If you are using urllib, your only choice is to put it in a separate
thread. The document doesn't really mention anything about the thread
safety of the pygame.event module, so I would assume the worst. The
easiest and safest way to pass the data between the threads is
probably by using the Queue module from Python
(http://www.python.org/doc/2.4.1/lib/module-Queue.html).

--
Sami Hangaslammi