Pyglet with Asyncio

280 views
Skip to first unread message

Chris Norman

unread,
Oct 1, 2020, 3:32:21 PM10/1/20
to pyglet-users
Hi all,
Is there an officially-supported way to use asyncio from within Pyglet?

If not, what's the recommended way to do networking with Pyglet?

Thanks in advance.

Take care,

Chris Norman

ryannathans

unread,
Oct 2, 2020, 5:14:09 AM10/2/20
to pyglet-users
I have a thread for all async stuff (eg networking) and run pyglet on the main thread (because windows seems to have problems if you run it in another thread - linux is ok with it though).

for example

def
run(self):
    with concurrent.futures.ThreadPoolExecutor() as thread_pool:
        #submit networking/async stuff to thread_pool
        try:
            pyglet.app.run()
        finally:
            #tell networking thread to shutdown
            atexit.unregister(concurrent.futures.thread._python_exit) # helps the program exit cleanly if there are blocking reads in threads that will never complete

I don't know if this is the "right way" but it works for me - just keep all the async in one thread and communicate with it in a threadsafe manner

Chris Norman

unread,
Oct 2, 2020, 6:58:20 AM10/2/20
to pyglet-users
OK, thank you very muc for that.

I need to learn about asyncio before I can even understand that code, but it looks about what I was expecting.

Hopefully if I can learn more about it, I can make some version of pyglet's event loop that'll cooperate nicely, but that'll do in the meantime. Thank you.

Take care,

Chris Norman



--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyglet-users/7cb132dc-3f3b-4e27-993f-7ad78f24f83an%40googlegroups.com.

Matan

unread,
Apr 18, 2023, 9:02:30 AM4/18/23
to pyglet-users
It's interesting that the mere use of threading does not break anything in pyglet's consistency as its own event loop's source reads a little like it assumes that none of its function calls would be preempted ― whereas python threading might be preempting function calls all of the time as it juggles the threads (or does it?). 

Anyone care to comment?

Reply all
Reply to author
Forward
0 new messages