为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

弑天下 2021-11-02 17:07:27
我需要制作 3 个线程来打印信号量颜色:按此顺序红色、黄色和绿色,然后,每个踏板必须随机休眠 N 秒。每种颜色必须为一个线程打印,所有这个过程必须重复 N 次。我试过将 lock.acquire() 和 lock.release() 移出 for 循环,但不起作用 =(from termcolor import coloredimport threadingimport timefrom random import randrangedef 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 = 5tempo = 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()简而言之,我的代码正在打印:但我需要这是显示的结果:
查看完整描述

2 回答

?
catspeake

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()


查看完整回答
反对 回复 2021-11-02
?
慕莱坞森

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,


查看完整回答
反对 回复 2021-11-02
  • 2 回答
  • 0 关注
  • 180 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信