[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Asynchronous IO with Pygame
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Asynchronous IO with Pygame
- From: Weeble <clockworksaint@xxxxxxxxx>
- Date: Sat, 29 Nov 2008 00:43:34 +0000
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Fri, 28 Nov 2008 19:43:37 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=2DFioHTxTRUF4bjo0/RcZ6mHvISqFDCcMBCvJyl5Hd4=; b=i+KShkRtEL7xbX9BPOOPoU0AbBetGEon/JYeTAPq6QV4zdceGhfbgtzTPe0G2Brv5D LcOyW5NX4mhMPcwtD1NDsz5HK3j1fg2QFU8pkvdjdEkgaZ96JUNxPp30XsCWbnsuPwo8 Fd3onZ2jFOIiXnDEEni2Ult3gPWAOiZcdKMzk=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=MptgQ+ko/7QcVg3sons7S6h+U/MYkx2APr6aFThQ5AlAs+f/99lOn9pkW1nmj7mBWa HTKQQXnW4s0l8ZfAFBVs3Os9f9zYkP0SllgIHnxBL/niqBjKqIw0TUj1b8bBWCzbB0/P BRs/SGFeJ6ffVacJNRrT+1tKSnomoKtl6m7Lw=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
Hi, I'm new to the list. Looking at the recent archive, I noticed that
somebody said that pygame.event.post is not thread-safe. Is this true?
I am trying to do asynchronous IO (communicating with another process)
and this was my only good solution.
For various reasons, I would like to run Pygame in one process and
send messages to it from another. One reason for this is so that I can
use an interactive Python shell to interact with my Pygame
application. I'm using multiprocessing and sending messages via an
interprocess queue. However, this gives me a dilemma in the Pygame
process. If I make a blocking call to event.get() or something like
that, I'll not be able to respond to messages coming in on the queue.
If I make a blocking call to read from the queue, I can't pump
messages. I can poll them constantly, but then I have to trade off
responsiveness with wasted CPU time. My application doesn't do
animation and I'd really like it not to be waking up unless it really
needs to. I thought I could achieve this by having a helper thread
that lives only to read from the queue and post Pygame events. Indeed,
this appears to work, but if event.post isn't guaranteed thread-safe
then I guess I may be inviting trouble.
Is this possible? Is there another way to get the same effect without
polling? Is this what the fastevent module is for?
I have uploaded an example here:
http://pastebin.com/f79e512b1
Save it as, e.g. example.py, then do:
import example
process, queue = example.start_pygame_process()
queue.put("Hello world!")
Any string you put on the queue will appear in the window, as well as
a counter that increments on each redraw.