控制python线程的调度优先级? [英] Controlling scheduling priority of python threads?

查看:1353
本文介绍了控制python线程的调度优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,该脚本使用两个线程池(每个线程池包含十个线程)从API提取数据.线程池在ActiveState上实现此代码.每个线程池都通过 PubSub 以获取新条目.发布新条目时,python将数据传递给使用python Subprocess的函数. POpen 执行PHP外壳以完成调用API的实际工作.

这种启动PHP Shell的系统对于我的PHP Web应用程序的功能是必不可少的,因此无法避免使用Python启动PHP Shell.

此脚本将仅在Linux服务器上运行.

如何控制广告的尼斯(计划优先级)应用程序的线程?

似乎无法在Python中控制单个线程的调度优先级.是否有python解决方案,或者至少可以与脚本一起运行的UNIX命令来控制优先级?

好吧,我最终没有找到处理它的python方法.我现在像这样很好地运行我的脚本:

nice -n 19 python MyScript.py

我相信python中的线程优先级是不可控的,因为它是如何使用全局解释器锁(GIL)来实现的.话虽如此,即使您可以给一个线程更多的CPU处理优先级,传递给GIL的python实现在传递给GIL时也不会意识到这一点.如果您能够在池中的单个线程中提高美观度(例如,它正在做更重要的工作),则您需要使用自己的锁实现来使更高优先级的线程更频繁地访问GIL.

通过Google搜索返回的这篇文章,我相信这与您的要求类似

解释为什么它不起作用 http://www.velocityreviews.com/forums/t329441-threading-priority.html

解释我建议的解决方法 http://bytes.com/topic/python/answers/645966-setting -thread-priorities

I've written a script that uses two thread pools of ten threads each to pull in data from an API. The thread pool implements this code on ActiveState. Each thread pool is monitoring a Redis database via PubSub for new entries. When a new entry is published, python passes the data to a function that uses python's Subprocess.POpen to execute a PHP shell to do the actual work of calling the API.

This system of launching PHP shells is necessary for functionality with my PHP web app, so launching PHP shells with Python can't be avoided.

This script will only be running on Linux servers.

How do I control the niceness (scheduling priority) of the application's threads?

Edit:

It seems controlling scheduling priority for individual threads in Python isn't possible. Is there a python solution, or at the very least a UNIX command I can run along with my script, to control the priority?

Edit 2:

Well I didn't end up finding a python way to handle it. I'm just running my script with nice now like this:

nice -n 19 python MyScript.py

解决方案

I believe that threading priority is not controllable in python due to how they are implemented using a global interpreter lock (GIL). Having said that, even if you could give one thread more CPU processing priority, the python implementation that hands around the GIL would not be aware of this as it handed around the GIL. If you were able to increase niceness in a single thread in your pool (say it is doing a more important job) you would need to use your own implementation of locks to give the higher priority thread access to the GIL more often.

A google search returns this article which I believe is similar to what you are asking

Explains why it doesnt work http://www.velocityreviews.com/forums/t329441-threading-priority.html

Explains the workaround I was suggesting http://bytes.com/topic/python/answers/645966-setting-thread-priorities

这篇关于控制python线程的调度优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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