如何使用threading.Thread在Python中产生其余的量程?

时间:2021-10-01 23:32:35

I've got a thread that's polling a piece of hardware.

我有一个线程正在轮询一块硬件。

while not hardware_is_ready():
    pass
process_data_from_hardware()

But there are other threads (and processes!) that might have things to do. If so, I don't want to burn up cpu checking the hardware every other instruction. It's been a while since I've dealt with threading, and when I did it wasn't Python, but I believe most threading libraries have a yield function or something that allows a thread to tell the scheduler "Give the other threads a chance."

但是还有其他线程(和进程!)可能有事情要做。如果是这样,我不希望每次其他指令都烧掉cpu检查硬件。自从我处理线程以来已经有一段时间了,当我这样做时,它不是Python,但我相信大多数线程库都有一个yield函数或允许线程告诉调度程序的东西“给其他线程一个机会。 “

while not hardware_is_ready():
    threading.yield()          # This function doesn't exist.
process_data_from_hardware()

But I can't find any reference to something like this in the threading documentation. Python does have a yield statement, but I'm pretty sure that's something else entirely (to do with generators).

但我在线程文档中找不到任何类似的引用。 Python确实有一个yield语句,但我很确定这完全是另一回事(与生成器有关)。

What's the correct thing to do here?

在这做什么是正确的?

3 个解决方案

#1


61  

time.sleep(0) is sufficient to yield control -- no need to use a positive epsilon. Indeed, time.sleep(0) MEANS "yield to whatever other thread may be ready".

time.sleep(0)足以产生控制 - 不需要使用正epsilon。实际上,time.sleep(0)意味着“屈服于任何其他线程可能已准备就绪”。

#2


12  

Read up on the Global Interpreter Lock (GIL).

阅读Global Interpreter Lock(GIL)。

For example: http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/

例如:http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/

Also: http://www.pyzine.com/Issue001/Section_Articles/article_ThreadingGlobalInterpreter.html

另外:http://www.pyzine.com/Issue001/Section_Articles/article_ThreadingGlobalInterpreter.html

Do this in your code if you must do Busy Waiting (e.g. polling a device).

如果您必须执行忙等待(例如,轮询设备),请在代码中执行此操作。

time.sleep( 0.0001 )

This will yield to the thread scheduler.

这将产生线程调度程序。

Also, I collected some notes and references in http://homepage.mac.com/s_lott/iblog/architecture/C551260341/E20081031204203/index.html

另外,我在http://homepage.mac.com/s_lott/iblog/architecture/C551260341/E20081031204203/index.html收集了一些说明和参考资料。

#3


3  

If you're doing this on *nix, you might find the select library useful. Kamaela also has a few components you may find useful, but it may require a bit of a paradigm change.

如果您在* nix上执行此操作,您可能会发现选择库很有用。 Kamaela也有一些你可能会觉得有用的组件,但它可能需要一些范例的改变。

#1


61  

time.sleep(0) is sufficient to yield control -- no need to use a positive epsilon. Indeed, time.sleep(0) MEANS "yield to whatever other thread may be ready".

time.sleep(0)足以产生控制 - 不需要使用正epsilon。实际上,time.sleep(0)意味着“屈服于任何其他线程可能已准备就绪”。

#2


12  

Read up on the Global Interpreter Lock (GIL).

阅读Global Interpreter Lock(GIL)。

For example: http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/

例如:http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/

Also: http://www.pyzine.com/Issue001/Section_Articles/article_ThreadingGlobalInterpreter.html

另外:http://www.pyzine.com/Issue001/Section_Articles/article_ThreadingGlobalInterpreter.html

Do this in your code if you must do Busy Waiting (e.g. polling a device).

如果您必须执行忙等待(例如,轮询设备),请在代码中执行此操作。

time.sleep( 0.0001 )

This will yield to the thread scheduler.

这将产生线程调度程序。

Also, I collected some notes and references in http://homepage.mac.com/s_lott/iblog/architecture/C551260341/E20081031204203/index.html

另外,我在http://homepage.mac.com/s_lott/iblog/architecture/C551260341/E20081031204203/index.html收集了一些说明和参考资料。

#3


3  

If you're doing this on *nix, you might find the select library useful. Kamaela also has a few components you may find useful, but it may require a bit of a paradigm change.

如果您在* nix上执行此操作,您可能会发现选择库很有用。 Kamaela也有一些你可能会觉得有用的组件,但它可能需要一些范例的改变。