【发布时间】:2015-05-15 09:55:45
【问题描述】:
这里有一些示例代码
while True: #main-loop
if command_received:
thread = Thread(target = doItNOW)
thread.start()
......
def doItNOW():
some_blocking_operations()
我的问题是我需要“some_blocking_operations”来立即启动(只要 command_received 为 True)。 但由于它们阻塞了我无法在我的主循环中执行它们 而且我也不能将“some_blocking_operations”更改为非阻塞
对于“立即”,我的意思是尽快,延迟不超过 10 毫秒。 (我曾经有一整秒的延迟)。 如果不可能,那么持续的延迟也是可以接受的。 (但它必须是恒定的。只有几毫秒的错误)
我目前正在开发一个 linux 系统(Ubuntu,但将来可能是另一个系统。总是 linux)
一个 python 解决方案会很棒.. 但一个不同的解决方案总比没有好
有什么想法吗? 提前谢谢
【问题讨论】:
-
您看到的延迟很可能是因为您只能有一个运行 python 代码的线程(GIL 和所有线程)。如果阻塞操作是 IO,你可以使用 gevent。
标签: python linux multithreading delay