Python 如何在填充列表时运行线程?
目前,我使用的系统如下:Python 如何在填充列表时运行线程?,python,python-3.x,Python,Python 3.x,目前,我使用的系统如下: class Processor(object): """ Makes sure that all operations the user requires to be processed are processed in order Also makes sure that the users are still pickle-able """ def __init__(self): self.tasks = []
class Processor(object):
"""
Makes sure that all operations the user requires to be processed are processed in order
Also makes sure that the users are still pickle-able
"""
def __init__(self):
self.tasks = []
self.killed = False
def begin_processing(self):
while not self.killed:
if not self.tasks:
pass
基本上,游戏中的用户将任务(本质上是一个线程.Timer
)附加到任务中。该系统的主要目的是确保实际用户对象是可pickle的
然而,这是非常低效的,因为它不断地检查队列中是否有任务
我宁愿使它只在任务附加到队列时运行。有什么办法可以做到这一点吗?我想你想要的是一条信息
不是我想要的。我想要一种基本上会导致“task added”(任务添加)事件导致队列开始处理的方法。线程将在队列上停止。get()
调用并等待新任务。如果你想做其他的计算,你应该在主要的过程中做。哦,这更像是我想的。谢谢您!
import queue
class Processor(object):
"""
Makes sure that all operations the user requires to be processed are processed in order
Also makes sure that the users are still pickle-able
"""
def __init__(self):
self.tasks = queue.Queue()
self.killed = False
def begin_processing(self):
while not self.killed:
task = self.tasks.get()