08_线程间通信 ITC[Python常见问题]

1.线程间的通信方式

    1.共享全局变量

    2.线程队列(queue)

    3.信号量(Semaphore)

2.共享全局变量进行通信

from threading import Thread
import time

g_nums = [11,22,33]


def work1(nums):
    nums.append(44)
    print("----in work1---",nums)


def work2(nums):
    #延时一会,保证t1线程中的事情做完
    time.sleep(1)
    print("----in work2---",nums)


t1 = Thread(target=work1, args=(g_nums,))
t1.start()
t2 = Thread(target=work2, args=(g_nums,))
t2.start()
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 08_线程间通信 ITC