【发布时间】:2015-10-09 02:35:35
【问题描述】:
几周前我开始使用 Python 进行编程,并尝试使用 Semaphores 同步两个简单线程,以用于学习目的。这是我所拥有的:
import threading
sem = threading.Semaphore()
def fun1():
while True:
sem.acquire()
print(1)
sem.release()
def fun2():
while True:
sem.acquire()
print(2)
sem.release()
t = threading.Thread(target = fun1)
t.start()
t2 = threading.Thread(target = fun2)
t2.start()
但它一直只打印 1。如何插入打印件?
【问题讨论】:
标签: python multithreading semaphore python-multithreading