Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在填充列表时运行线程?_Python_Python 3.x - Fatal编程技术网

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()