2 回答
TA贡献1111条经验 获得超0个赞
这是相同代码的另一个版本,但使用纯信号量,而不是事件
from termcolor import colored
import threading
import time
from random import randrange
semaforo1 = threading.Semaphore()
semaforo2 = threading.Semaphore()
semaforo3 = threading.Semaphore()
def printThread(color):
duration = randrange(1, 11)
print(colored(color, 'grey', 'on_'+color, attrs=['dark', 'bold']))
print(colored("I'm going sleep for %d seconds!," % duration, color))
time.sleep(duration)
def threadRed(n):
semaforo2.acquire()
semaforo3.acquire()
for i in range(n):
semaforo1.acquire()
printThread("red")
#semaforo1.release()
semaforo2.release()
def threadYellow(n):
for i in range(n):
semaforo2.acquire()
printThread("yellow")
semaforo3.release()
def threadGreen(n):
for i in range(n):
semaforo3.acquire()
printThread("green")
semaforo1.release()
loop_count = 5
t_red = threading.Thread(target=threadRed, args=(loop_count,))
t_yellow = threading.Thread(target=threadYellow, args=(loop_count,))
t_green = threading.Thread(target=threadGreen, args=(loop_count,))
t_red.start()
t_yellow.start()
t_green.start()
t_red.join()
t_yellow.join()
t_green.join()
TA贡献1810条经验 获得超4个赞
好的,你想要的输出会杀死multi-threading.
为什么?
一个线程有一个开始、一个执行顺序和一个结论。它有一个指令指针,用于跟踪当前正在运行的上下文中的位置。
继续下去,您的问题的重点是同时运行多个线程,而不管哪个function先执行。
你正在尝试的是:
以顺序方式运行线程,无论哪个线程先完成,都不是multi-threading。最重要的是,您lock.acquire() and lock.release()在每个函数中使用将灵魂从多线程中取出
答:如果你真的想让线程以顺序方式运行,你不应该在函数内部获取和释放线程,
一点点变化:
from termcolor import colored
import threading
import time
from random import randrange
def threadRed(n, tempo):
for i in range(n):
# lock.acquire()
print(colored("red", 'grey', 'on_red', attrs=['dark', 'bold']))
print(colored("I'm going to sleep for %d seconds," % tempo, 'red'))
time.sleep(tempo)
# lock.release()
def threadYellow(n, tempo):
for i in range(n):
# lock.acquire()
print(colored("yellow", 'grey', 'on_yellow', attrs=['dark', 'bold']))
print(colored("I'm going to sleep for %d seconds," % tempo, 'yellow'))
time.sleep(tempo)
# lock.release()
def threadGreen(n, tempo):
for i in range(n):
# lock.acquire()
print(colored("green", 'grey', 'on_green', attrs=['dark', 'bold']))
print(colored("I'm going to sleep for %d seconds," % tempo, 'green'))
time.sleep(tempo)
# lock.release()
lock = threading.Lock()
repeticoes = 5
lock.acquire()
tempo = randrange(1, 11)
t_red = threading.Thread(target=threadRed, args=(repeticoes, tempo))
tempo = randrange(1, 11)
t_yellow = threading.Thread(target=threadYellow, args=(repeticoes, tempo))
tempo = randrange(1, 11)
t_green = threading.Thread(target=threadGreen, args=(repeticoes, tempo))
lock.release()
t_red.start()
t_yellow.start()
t_green.start()
t_red.join()
t_yellow.join()
t_green.join()
输出:
red
I'm going to sleep for 6 seconds,
yellow
I'm going to sleep for 4 seconds,
green
I'm going to sleep for 4 seconds,
yellow
I'm going to sleep for 4 seconds,
green
I'm going to sleep for 4 seconds,
red
I'm going to sleep for 6 seconds,
yellow
I'm going to sleep for 4 seconds,
green
I'm going to sleep for 4 seconds,
red
I'm going to sleep for 6 seconds,
添加回答
举报