简体   繁体   English

是否有解决方法可以在单独的线程中清空事件队列?

[英]Is there a workaround to empty event queue in separate thread?

I used a separate thread to call pygame.event.get() for ages on Ubuntu without any problem.我使用一个单独的线程在 Ubuntu 上调用pygame.event.get()没有任何问题。 However MacOS throws an exception 'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called from the Main Thread!'然而,MacOS 会抛出异常'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called from the Main Thread!' ; ; and Windows reports the window to be (Not answering) after a few seconds of normal execution, which I assume to have the same explanation, meaning that Linux is tolerant to this software design unlike other OSes.并且 Windows 在正常执行几秒钟后报告窗口为(Not answering) ,我认为这有相同的解释,这意味着与其他操作系统不同,Linux 可以容忍这种软件设计。

Is there any workaround to keep emptying the event queue in a separate thread, or does it basically mean that pygame 1.9.1 on python 2.7 cannot work that way at all?是否有任何解决方法可以在单独的线程中保持清空事件队列,或者这是否基本上意味着 python 2.7 上的 pygame 1.9.1 根本无法以这种方式工作?

I know GUI events are usually handled in main thread, but my Python lib is intended to be used with ipython.我知道 GUI 事件通常在主线程中处理,但我的 Python 库旨在与 ipython 一起使用。 By design, the main thread, ie the user's ipython cells might be executing no operation or CPU-greedy loops, thus it can't take care of pygame.event.get() .根据设计,主线程,即用户的 ipython 单元可能不执行任何操作或 CPU 贪婪循环,因此它无法处理pygame.event.get()

It is only possible to post events from other thread, but the event queue has to be handled in the main thread.只能从其他线程发布事件,但事件队列必须在主线程中处理。 See the documentation of pygame.event :请参阅pygame.event的文档:

[...] The event subsystem should be called from the main thread. [...] 事件子系统应该从主线程调用。 [...] [...]

However, if you read on但是,如果您继续阅读

[...] If you want to post events into the queue from other threads, please use the pygame.fasteventpygame module for interacting with events and queues module. [...] 如果您想从其他线程将事件发布到队列中,请使用pygame.fasteventpygame模块与事件和队列模块进行交互。 [...] [...]

See pygame.fastevent module:参见pygame.fastevent模块:

pygame.fastevent is a wrapper for Bob Pendleton's fastevent library. pygame.fastevent是 Bob Pendleton 的fastevent库的包装器。 It provides fast events for use in multithreaded environments.它提供了用于多线程环境的快速事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单独的线程中启动 asyncio 事件循环并消耗队列项 - Start asyncio event loop in separate thread and consume queue items 在队列不为空时停止 python 线程 - Stopping a python thread while the queue is not empty 从工作线程添加到主线程后,该队列为空 - Queue in main thread is empty after adding to it from worker thread Flask:后台线程将非空队列视为空 - Flask: Background thread sees a non-empty queue as empty Python-单独线程上的非空共享列表显示为空 - Python - Non-empty shared list on separate thread appears empty 使用事件对象将线程作为单独的模块无法停止Python中的线程 - cannot stop a thread in Python, using Event Objects, thread as separate module 如何在退出主线程之前确保队列为空 - How to make sure queue is empty before exiting main thread 即使从Python中的另一个线程填充,队列也始终为空 - Queue is always empty, even when filled from another thread in Python multiprocessing.Queue - 如果在 `queue.get()` 之前没有调用`queue.empty()`,单个线程会挂在`queue.get()` 上吗? - multiprocessing.Queue - Will a single thread hang on `queue.get()` if not `queue.empty()` is called before `queue.get()`? 队列和线程,而在线程中循环 - queue and thread, while loop in thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM