【发布时间】:2013-06-17 08:41:53
【问题描述】:
我正在为 Java 的 tryAcquire Semaphore 函数寻找 python 替代方案。我发现这个函数是在 python 版本 3 及更高版本中添加的。我正在使用 python 版本 2.6.5。我有什么选择吗?我这里只有 semaphore.acquire(blocking=False) 这是我的 Java 代码 - (信号量发布在另一个我没有包含代码的线程中完成)
if(Sem.tryAcquire(30, TimeUnit.SECONDS))
log.info("testCall Semaphore acquired ");
else
log.error("Semaphore Timeout occured");
【问题讨论】:
-
threading.Lock可以工作吗?看看here。 -
No threading.Lock 也类似于 threading.semaphore。不提供超时!我在想可能是我可以使用 sem.acquire 和使用 threading.timer 循环。但是计时器在其他线程中唤醒。调用 timer.start 后,我的主线程将继续执行,这就是 timer 的问题!
标签: python multithreading semaphore blocking