Python 上的信号量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31508574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Semaphores on Python
提问by Victor Turrisi
I've started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what I've got:
几周前我开始用 Python 编程,并尝试使用信号量来同步两个简单的线程,以供学习之用。这是我所拥有的:
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()
But it keeps printing just 1's. How can I intercale the prints?
但它一直只打印 1。如何插入打印件?
回答by Anand S Kumar
It is working fine, its just that its printing too fast for you to see . Try putting a time.sleep()
in both functions (a small amount) to sleep the thread for that much amount of time, to actually be able to see both 1 as well as 2.
它工作正常,只是它的打印速度太快,你看不到。尝试time.sleep()
在两个函数(少量)中放入 a以使线程休眠那么多时间,实际上能够同时看到 1 和 2。
Example -
例子 -
import threading
import time
sem = threading.Semaphore()
def fun1():
while True:
sem.acquire()
print(1)
sem.release()
time.sleep(0.25)
def fun2():
while True:
sem.acquire()
print(2)
sem.release()
time.sleep(0.25)
t = threading.Thread(target = fun1)
t.start()
t2 = threading.Thread(target = fun2)
t2.start()
回答by Tim Seed
I used this code to demonstrate how 1 thread can use a Semaphore and the other thread will wait (non-blocking) until the Sempahore is available.
我使用此代码来演示 1 个线程如何使用信号量,而另一个线程将等待(非阻塞)直到信号量可用。
This was written using Python3.6; Not tested on any other version.
这是使用 Python3.6 编写的;未在任何其他版本上测试。
This will only work is the synchronization is being done from the same thread, IPC from separate processes will fail using this mechanism.
这只会在同步从同一个线程完成时起作用,来自不同进程的 IPC 将使用此机制失败。
import threading
from time import sleep
sem = threading.Semaphore()
def fun1():
print("fun1 starting")
sem.acquire()
for loop in range(1,5):
print("Fun1 Working {}".format(loop))
sleep(1)
sem.release()
print("fun1 finished")
def fun2():
print("fun2 starting")
while not sem.acquire(blocking=False):
print("Fun2 No Semaphore available")
sleep(1)
else:
print("Got Semphore")
for loop in range(1, 5):
print("Fun2 Working {}".format(loop))
sleep(1)
sem.release()
t1 = threading.Thread(target = fun1)
t2 = threading.Thread(target = fun2)
t1.start()
t2.start()
t1.join()
t2.join()
print("All Threads done Exiting")
When I run this - I get the following output.
当我运行它时 - 我得到以下输出。
fun1 starting
Fun1 Working 1
fun2 starting
Fun2 No Semaphore available
Fun1 Working 2
Fun2 No Semaphore available
Fun1 Working 3
Fun2 No Semaphore available
Fun1 Working 4
Fun2 No Semaphore available
fun1 finished
Got Semphore
Fun2 Working 1
Fun2 Working 2
Fun2 Working 3
Fun2 Working 4
All Threads done Exiting
回答by Benyamin Jafari
You can also use Lock/mutexmethod as following:
您还可以使用Lock/mutex方法如下:
import threading
import time
mutex = threading.Lock() # equal to threading.Semaphore(1)
def fun1():
while True:
mutex.acquire()
print(1)
mutex.release()
time.sleep(0.5)
def fun2():
while True:
mutex.acquire()
print(2)
mutex.release()
time.sleep(0.5)
t1 = threading.Thread(target=fun1).start()
t2 = threading.Thread(target=fun2).start()
Another/Simpler usage type:
另一种/更简单的使用类型:
import threading
import time
mutex = threading.Lock() # equal to threading.Semaphore(1)
def fun1():
while True:
with mutex:
print(1)
time.sleep(0.5)
def fun2():
while True:
with mutex:
print(2)
time.sleep(0.5)
t1 = threading.Thread(target=fun1).start()
t2 = threading.Thread(target=fun2).start()
[NOTE]:
[注意]:
In addition, the difference between mutex, semaphore, and lock
另外,互斥量、信号量、锁的区别