如何让3个线程按顺序打印

我需要制作 3 个线程来打印信号量颜色:按此顺序红色、黄色和绿色,然后,每个踏板必须随机休眠 N 秒。每种颜色必须为一个线程打印,所有这个过程必须重复 N 次。


我试过将 lock.acquire() 和 lock.release() 移出 for 循环,但不起作用 =(


from termcolor import colored

import threading

import time

from random import randrange



def threadRed(n, lock, 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, lock, 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, lock, 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

tempo = randrange(1, 11)

t_red = threading.Thread(target=threadRed, args=(repeticoes, lock, tempo))

tempo = randrange(1, 11)

t_yellow = threading.Thread(target=threadYellow, args=(repeticoes, lock, tempo))

tempo = randrange(1, 11)

t_green = threading.Thread(target=threadGreen, args=(repeticoes, lock, tempo))


t_red.start()

t_yellow.start()

t_green.start()

t_red.join()

t_yellow.join()

t_green.join()

简而言之,我的代码正在打印:

http://img3.mukewang.com/6180ffe70001b58202310311.jpg

但我需要这是显示的结果:

http://img.mukewang.com/6180fff40001f72402410376.jpg


弑天下
浏览 181回答 2
2回答

catspeake

这是相同代码的另一个版本,但使用纯信号量,而不是事件from termcolor import coloredimport threadingimport timefrom random import randrangesemaforo1 = 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 = 5t_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()

慕莱坞森

好的,你想要的输出会杀死multi-threading.为什么?一个线程有一个开始、一个执行顺序和一个结论。它有一个指令指针,用于跟踪当前正在运行的上下文中的位置。继续下去,您的问题的重点是同时运行多个线程,而不管哪个function先执行。你正在尝试的是:以顺序方式运行线程,无论哪个线程先完成,都不是multi-threading。最重要的是,您lock.acquire() and lock.release()在每个函数中使用将灵魂从多线程中取出答:如果你真的想让线程以顺序方式运行,你不应该在函数内部获取和释放线程,一点点变化:from termcolor import coloredimport threadingimport timefrom random import randrangedef 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 = 5lock.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()输出:redI'm going to sleep for 6 seconds,yellowI'm going to sleep for 4 seconds,greenI'm going to sleep for 4 seconds,yellowI'm going to sleep for 4 seconds,greenI'm going to sleep for 4 seconds,redI'm going to sleep for 6 seconds,yellowI'm going to sleep for 4 seconds,greenI'm going to sleep for 4 seconds,redI'm going to sleep for 6 seconds,
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python