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旋轉起來而不會死亡。 但我不確定這個意圖。你希望基本上有一個等待分配任務的線程池,對嗎? 但是,您的主函數以及您的工作函數(線程要運行的東西)會激活線程。您不應該從線程內激活線程。 –