2
我想運行兩個線程,每個線程都有一個參數傳遞給它來處理。但是,看起來線程按順序運行,而不是並行運行。證人:Python中的線程與發送到線程的參數
$ cat threading-stackoverflow.py
import threading
class CallSomebody (threading.Thread):
def __init__(self, target, *args):
self._target = target
self._args = args
threading.Thread.__init__(self)
def run (self):
self._target(*self._args)
def call (who):
while True:
print "Who you gonna call? %s" % (str(who))
a=CallSomebody(call, 'Ghostbusters!')
a.daemon=True
a.start()
a.join()
b=CallSomebody(call, 'The Exorcist!')
b.daemon=True
b.start()
b.join()
$ python threading-stackoverflow.py
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
我希望有一些線路返回Ghostbusters!
和其他人返回The Exorcist!
,但是Ghostbusters!
線永遠持續下去。必須重構什麼才能讓每個線程獲得一些處理器時間?
+1 - 正要點擊帖子:) – RocketDonkey
謝謝!就是這樣! – dotancohen