AlexElvers / pygame-with-asyncio

pygame with asyncio
15 stars 1 forks source link

Hangs with pygame 2.0.1 #1

Open jacekt opened 3 years ago

jacekt commented 3 years ago

Example app runs for couple of seconds (with mouse pointer changed to "application busy") then hangs completely. According to pygame documentation *) The event subsystem should be called from the main thread so that's probably the reason.

*) https://www.pygame.org/docs/ref/event.html

AlexElvers commented 3 years ago

I'm sorry, I cannot reproduce the error on my computer using Linux. However, you might be right about the reason why it does not work for you since run_in_executor uses a thread pool.

Since pygame.event.wait() is blocking, you cannot use it in the thread that is running the other coroutines without slowing them down. Instead, you can use pygame.event.poll():

async def pygame_event_loop(event_queue):
    while True:
        await asyncio.sleep(0)  # allow other tasks to run
        event = pygame.event.poll()
        if event.type != pygame.NOEVENT:
            await event_queue.put(event)

def main():
    ...
    pygame_task = asyncio.ensure_future(pygame_event_loop(event_queue))
    ...