好吧,我在很多地方都读到 pygame 事件处理必须在主线程中完成.我想这样做,但我的问题是,我可以从不同的线程向事件队列添加事件吗?
Well I have read at various places that the pygame event handling must be done in the main thread. I want to do that but my question is, can I add events to the event queue from a different thread?
我想从不同的线程调用 pygame.event.post(myEvent) 而不是在主循环中处理事件.这可能吗?
I want to call pygame.event.post(myEvent) from a different thread and than handle the event in the main loop. Is this possible?
澄清一下,我想为异步网络 i/o 运行一个单独的线程.当新消息到达时,线程会在事件队列中放置一个事件,以表示有事情要做.
edit: To clarify, I want to run a separate thread for asynchronous network i/o. When a new message arrived the thread would then put an event in the event queue to signal there is something to do.
推荐答案是时候进行一些侦探工作了!
Time for some detective work!
在 event_post 的源代码"noreferrer">event.c 表示 C 函数使用 SDL 调用 SDL_PushEvent,而不自行检查线程安全.但是,SDL_PushEvent 的文档说:
Looking at the source for event_post in event.c indicates that the C function uses the SDL call SDL_PushEvent, without checking for thread-safety on its own. However, the documentation for SDL_PushEvent says:
该函数是线程安全的,可以安全地从其他线程调用.
This function is thread safe, and can be called from other threads safely.
所以看起来确实是线程安全的.
So it seems that it is indeed thread-safe.
更多推荐
我可以从第二个线程添加 pygame 事件吗
发布评论