0
我已经使用了很多这个功能,并且找不到任何答案 - 因此我在问。并发线程正在等待任务
现在已经过去了一天,但我无法了解一些线程概念,这可能是为什么我的代码很混乱。
我生成3个主题。精细。
当线程2产生时,线程1“停止”,我假设它意味着它死亡。线程2和3相同。
我将这些线程放入活动池中。
我在努力的是保持所有3个线程同时运行并等待。我想有一个随机的时间间隔为任务分配线程的方法。
从我收集的,我的线程正在死亡的原因是因为我的工人类正在返回。然而,玩过它并把它放在一个循环中(while 1),我仍然无法获得任何工作。
任何想法?
import logging
import random
import threading
import time
logging.basicConfig(level = logging.DEBUG, format = '(%(threadName)-2s) %(message)s')
class ActivePool(object):
def __init__(self):
super(ActivePool, self).__init__()
self.active = []
self.lock = threading.Lock()
def activate(self, name):
with self.lock:
self.active.append(name)
logging.debug('Running wheel: %s', self.active)
self.move(name)
def move(self, name):
while name.is_alive():
logging.debug('yes')
def inactive(self, name):
with self.lock:
self.active.remove(name)
logging.debug('Running wheel: %s', self.active)
def rtime(self):
self.rt = random.randint(5, 10)
t = threading.Timer(rt, self.revent)
def join(self):
for t in self.active:
t.join()
def check(self):
for t in self.active:
if t.is_alive():
print t
def worker(s, pool):
logging.debug('Wheel inactive')
with s:
#name = threading.currentThread().getName()
thread = threading.currentThread()
logging.debug('ACTIVATING')
pool.activate(thread)
#time.sleep(2)
#pool.inactive(thread)
if __name__ == "__main__":
pool = ActivePool()
s = threading.Semaphore()
for i in range(0, 6):
t = threading.Thread(target = worker, name = str(i + 1), args = (s, pool))
pool.activate(t)
t.start()
logging.debug('here')
即使在这里添加一些伪代码也会有所帮助。否则会非常难以诊断。 –
@phyllisdiller新增 – popopret
当我清理一些东西时,我会让线程1旋转起来而不会死亡。 但我不确定这个意图。你希望基本上有一个等待分配任务的线程池,对吗? 但是,您的主函数以及您的工作函数(线程要运行的东西)会激活线程。您不应该从线程内激活线程。 –