Python - 可以从特定线程解锁信号量获取吗?
Python - Can semaphore Acquire be unlocked from a specific thread?
几周前我开始 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 有时间用 .acquire
阻塞线程 1)。如果我没有 time.sleep
,结果将不一致。
我现在得到了很好的结果,但我希望我的线程 2 总是在线程 1 开始 "Code1" 之前开始它的 "if",所以我可以删除那个 time.sleep(0.001)
并获得一致的结果。
你有什么想法吗?
您要求同步启动行为。为此,您当前不适合使用信号量。您编写的代码清楚地表明您不关心 运行 首先是哪个进程。这也是处理事物的标准方式,因此如果您需要一个线程先于另一个线程,则可能需要一种不同的同步机制。如果我更了解你潜在的欲望,我可以告诉你更多。
但是,根据您当前的代码,您想要实现的目标将使用第二种机制来完成,即在第一个线程甚至尝试获取信号量之前阻塞它,并且一旦它被第二个线程释放在其关键代码块内,e。 G。一个 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()
但这对我来说似乎很粗糙而且容易出错。如果你告诉我们你真正想要实现什么,你最初试图解决什么问题,答案可能会改进很多。
几周前我开始 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 有时间用 .acquire
阻塞线程 1)。如果我没有 time.sleep
,结果将不一致。
我现在得到了很好的结果,但我希望我的线程 2 总是在线程 1 开始 "Code1" 之前开始它的 "if",所以我可以删除那个 time.sleep(0.001)
并获得一致的结果。
你有什么想法吗?
您要求同步启动行为。为此,您当前不适合使用信号量。您编写的代码清楚地表明您不关心 运行 首先是哪个进程。这也是处理事物的标准方式,因此如果您需要一个线程先于另一个线程,则可能需要一种不同的同步机制。如果我更了解你潜在的欲望,我可以告诉你更多。
但是,根据您当前的代码,您想要实现的目标将使用第二种机制来完成,即在第一个线程甚至尝试获取信号量之前阻塞它,并且一旦它被第二个线程释放在其关键代码块内,e。 G。一个 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()
但这对我来说似乎很粗糙而且容易出错。如果你告诉我们你真正想要实现什么,你最初试图解决什么问题,答案可能会改进很多。