• Python多线程


    import _thread #线程
    import time

    signal_value = 0

    def th1():
    a = 0
    global signal_value
    while(1):
    if(signal_value == 0):
    print("a")
    signal_value = 1
    a+=1
    if(a > 9):
    break
    time.sleep(1)
    return

    def th2():
    a = 0
    global signal_value
    while(1):
    if(signal_value == 1):
    print("b")
    signal_value = 2
    a+=1
    if(a > 9):
    break
    time.sleep(1)
    return

    def th3():
    a = 0
    global signal_value
    while(1):
    if(signal_value == 2):
    print("c")
    signal_value = 3
    a+=1
    if(a > 9):
    break
    time.sleep(1)
    return

    def th4():
    a = 0
    global signal_value
    while(1):
    if(signal_value == 3):
    print("d")
    signal_value = 0
    a+=1
    if(a > 9):
    break
    time.sleep(1)
    return

    def main():
    try:
    _thread.start_new_thread( th1, ())
    _thread.start_new_thread( th2, ())
    _thread.start_new_thread( th3, ())
    _thread.start_new_thread( th4, ())
    except Exception as e:
    print("---异常---:", e)
    while 1:
    pass

    return

    main()

    还需完善修改

  • 相关阅读:
    网络流 方阵移动
    NOI2019滚粗记
    PKUSC2019游记
    CQOI十二省联考游记
    数学结论题 书堆
    计算几何 大灾变
    51NOD 1773 A国的贸易
    BZOJ 3944
    51Nod 1238
    NOIP2018游记
  • 原文地址:https://www.cnblogs.com/txzing/p/14651960.html
Copyright © 2020-2023  润新知