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

Re: Re: [pygame] BUG: pygame.event.peek



This does not appear to be the case. Check out the "pygame+threading+windows" thread on this list. Other threads can't "get" an event behind your back because they cannot get an event. So, perhaps by accident rather than design, peek is thread safe.

peek is useful in order to service events while also doing other processing. Pseudocode:

while not done:
   #each part will take a second or two
   do_part_of_a_long_computation()

   #service events if there are any, otherwise do another part
   # of the long computation...
   if event.peek():
       for event in event.get():
           process_event(event)

You might prefer to have do_part_of_a_long_computation run on another thread, but you may not have that luxury for whatever reason. Maybe it needs access to the event queue for some reason, for example...

Another use that comes to mind is to yield time to other processes, something like this (pseudo code again):

while running:
   if not event.peek():
       #let something else run for a bit...
       sleep(1)
   else:
       process_events()

A final example uses peek's abaility to just see if certain event types are in the event queue:

#assumes pygame.locals imported
while app.running:
   #process mouse events only
   if event.peek([MOUSEMOTION, MOUSEDOWN, MOUSEUP])
       do_mouse_things()
   #don't care about other events...

peek has its place.

Regards,
Taylor

----- Original Message ----- From: "René Dudfield" <renesd@xxxxxxxxx>
To: <pygame-users@xxxxxxxx>
Sent: Monday, July 31, 2006 4:31 AM
Subject: [code] Re: [pygame] BUG: pygame.event.peek



On 7/31/06, Brian Fisher <brian@xxxxxxxxxxxxxxxxxxx> wrote:
On 7/30/06, Tim Ansell <mithro@xxxxxxxxxx> wrote:
> since peek is a kind of silly unthread safe function, maybe that
> functionality should be removed.
>
Rene, what do you mean about it being unthread safe? are you just
talking about the implementation of it, or something about the idea of
peeking at the event queue? I don't see how peeking the queue could be
inherently less thread-safe than pulling events off


Hi,

I think because you peek at the event something could in the mean time
get the event.  I haven't tested this myself, but this is from the
report of the fastevent guy that tested the threadsafeness of SDL
events.  It's written somewhere on his page.