是否有类似于Python中Java的Thread.yield()的东西?那有道理吗? [英] In there something similar to Java's Thread.yield() in Python? Does that even make sense?

查看:264
本文介绍了是否有类似于Python中Java的Thread.yield()的东西?那有道理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想告诉我的Python线程屈服,因此避免不必要地占用CPU.在Java中,您可以使用Thread.yield()函数来实现.我认为Python中没有类似的东西,所以我一直在使用time.sleep(t)其中t = 0.00001.对于t=0似乎没有任何作用.

我认为关于Python的线程模型,也许有一些我不正确的理解,因此缺少thread.yield()的原因.有人可以向我澄清吗?谢谢!

PS:这是Java Thread.yield()的文档所说的:

导致当前正在执行的线程 对象暂时暂停并允许 其他要执行的线程.

解决方案

无论如何,解释器都会定期从一个线程切换到另一个线程,而无需您的干预-您不需要告诉系统不要占用"线程. /p>

但是,在正常情况下,任何时候只有一个Python线程正在执行. (异常往往围绕线程等待来自外部设备(例如硬盘或网络)的输入而发生.)这是由于全局解释器锁定.但是,这的确意味着您从Python线程中获得的收益可能不如Java或许多其他语言那么多.解决这个问题不一定很简单,尽管转向多处理而不是多线程是一个问题好的方法,如果可能的话.

但是,您想要做的事情在某种意义上似乎是有缺陷的-如果您有2个线程并且它们都有工作要做,那么您不必编写应用程序侧代码即可在它们之间进行切换.这就是操作系统或虚拟机的工作.如果您发现自己想让一个线程少做点事情"是因为您希望在处理器时间方面偏向于另一个线程,那么您可能应该设置线程优先级,而不是添加任意的让步或休眠. (尽管如此,考虑到全局解释器锁的存在,这在Python中可能没有多大意义.)

I want to tell my Python threads to yield, and so avoid hogging the CPU unnecessarily. In Java, you could do that using the Thread.yield() function. I don't think there is something similar in Python, so I have been using time.sleep(t) where t = 0.00001. For t=0 there seems to be no effect.

I think that maybe there is something I am not understanding correctly about Python's threading model, and hence the reason for the missing thread.yield(). Can someone clarify this to me? Thanks!

PS: This is what the documentation for Java's Thread.yield() says:

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

解决方案

The interpreter will switch from one thread to another periodically anyway without your intervention - you don't need to tell the system not to 'hog' a thread.

However, under normal circumstances, only one Python thread is executing at any one time. (Exceptions tend to revolve around times when threads are waiting on input from external devices such as the hard disk or the network.) This is due to the Global Interpreter Lock. This does mean however that you probably aren't getting as much benefit from threads in Python as you would in Java or many other languages. Getting around this problem is not necessarily trivial, although moving to multiprocessing instead of multithreading is one good approach, if possible.

However, what you want to do seems flawed in some sense - if you have 2 threads and they both have work to do, you shouldn't have to write application-side code to switch between them. That's the job of the operating system or the virtual machine. If you find yourself telling one thread to 'do less' because you want to favour another thread in terms of processor time, then rather than adding arbitrary yielding or sleeping, you should probably be setting thread priorities instead. (Although again, this may not have much meaning in Python given the presence of the Global Interpreter Lock.)

这篇关于是否有类似于Python中Java的Thread.yield()的东西?那有道理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆