python - Python - 可以从特定线程解锁信号量获取吗?
问题描述
几周前我出于学习目的开始使用 Python。我想知道是否可以从特定线程解锁信号量获取。还是有其他工具?
import threading, time
sem = threading.Semaphore
def thread1 (threadname):
#Code to do
#Condition thread1
time.sleep(0.001)
sem.acquire()
#Code 1
sem.release
def thread2 (threadname):
while (thread1.is_alive() == True):
if #Condition thread1
sem.acquire()
#Code 2
sem.release
我在线程 1 中的情况正好在 time.sleep 之前(所以线程 2 有时间用 阻塞 thread1 .acquire
)。如果我没有那个time.sleep
,结果并不一致。我现在得到了很好的结果,但我希望我的线程 2 总是在线程 1 开始“Code1”之前开始它的“if”,所以我可以删除它time.sleep(0.001)
并获得一致的结果。
你有什么主意吗 ?
解决方案
您要求同步启动行为。为此,您当前使用的信号量不适合。您编码的内容清楚地表明您不关心哪个进程首先运行。这也是处理事物的标准方式,因此如果您需要在另一个线程之前拥有一个线程,则可能需要一种不同的同步机制。如果我更多地了解你的潜在愿望,我可以告诉你更多。
但是,根据您当前的代码,您想要实现的目标将使用第二种机制来完成,即在第一个线程甚至尝试获取信号量之前阻塞它,并且一旦第二个线程在其关键代码中就将其释放块,例如 a threading.Event
:
#!/usr/bin/env python3
import threading, time
semaphore = threading.Semaphore()
event = threading.Event()
event.clear()
def action1():
print("starting thread1")
time.sleep(0.1)
print("waiting in thread1 ...")
event.wait()
print("woken up in thread1!")
print("acquiring in thread1 ...")
semaphore.acquire()
print("critical in thread 1")
semaphore.release()
print("leaving thread1")
def action2():
print("starting thread2")
while (threads[0].is_alive()):
print("acquiring in thread2 ...")
semaphore.acquire()
print("critical in thread 2")
event.set()
time.sleep(0.1)
semaphore.release()
print("released in thread2")
print("leaving thread2")
threads = [ threading.Thread(target=action1),
threading.Thread(target=action2) ]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
但这对我来说似乎很粗糙,容易出错。如果你告诉我们你真正想要达到什么,你最初试图解决什么问题,答案可能会改进很多。
推荐阅读
- neural-network - 如何在 pytorch 中使用 tensorboard 调试器?
- r - R中geom_polygon中“填充”参数的行为
- ios - Xcode 版本 10.3 ITMS-90035:签名无效
- azure-sql-database - Azure PaaS SQL 密码策略
- java - HTML 内容不同于 Google“查看页面源代码”
- android - Android SDK 让我的 MacBook Pro 关机
- python - 删除可能不必要的代码行
- c - 希望简化 C 中的 if 语句链
- java - 绘制一个向鼠标单击点缓慢移动而不是跳转到它的矩形(JAVA)
- cuda - CUDA 线程块中的线程如何拆分成 warp?