当前位置: 首页 > news >正文

江西省城乡建设厅网站app拉新渠道

江西省城乡建设厅网站,app拉新渠道,免费做网站排名,webapi做网站问题 你已经听说过全局解释器锁 GIL,担心它会影响到多线程程序的执行性能。 解决方案 尽管 Python 完全支持多线程编程,但是解释器的 C 语言实现部分在完全并行执行时并不是线程安全的。 实际上,解释器被一个全局解释器锁保护着&#xff…

问题

你已经听说过全局解释器锁 GIL,担心它会影响到多线程程序的执行性能。

解决方案

尽管 Python 完全支持多线程编程,但是解释器的 C 语言实现部分在完全并行执行时并不是线程安全的。

实际上,解释器被一个全局解释器锁保护着,它确保任何时候 都只有一个 Python 线程执行。

GIL 最大的问题就是 Python 的多线程程序并不能利用 多核 CPU 的优势(比如一个使用了多个线程的计算密集型程序只会在一个单 CPU 上 面运行)。

在讨论普通的 GIL 之前,有一点要强调的是 GIL 只会影响到那些严重依赖 CPU 的程序(比如计算型的)。

如果你的程序大部分只会涉及到 I/O,比如网络交互,那么 使用多线程就很合适,因为它们大部分时间都在等待。

实际上,你完全可以放心的创建 几千个 Python 线程,现代操作系统运行这么多线程没有任何压力,没啥可担心的。

而对于依赖 CPU 的程序,你需要弄清楚执行计算的特点。

例如,优化底层算法 要比使用多线程运行快得多。类似的,由于 Python 是解释执行的,如果你将那些性能 瓶颈代码移到一个 C 语言扩展模块中,速度也会提升的很快。

如果你要操作数组,那 么使用 NumPy 这样的扩展会非常的高效。

还有一点要注意的是,线程不是专门用来优化性能的。一个 CPU 依赖型程序可能 会使用线程来管理一个图形用户界面、一个网络连接或其他服务。这时候,GIL 会产生 一些问题,因为如果一个线程长期持有 GIL 的话会导致其他非 CPU 型线程一直等待。

事实上,一个写的不好的 C 语言扩展会导致这个问题更加严重,尽管代码的计算部分 会比之前运行的更快些。

说了这么多,现在想说的是我们有两种策略来解决 GIL 的缺点。

首先,如果你完 全工作于 Python 环境中,你可以使用 multiprocessing 模块来创建一个进程池,并像协同处理器一样的使用。

例如,你有如下的线程代码:

#执行大型计算(CPU限制)

def some_work(args):
...return result

调用上述函数的线程

def some_thread():while True:...r = some_work(args)...修改代码,使用进程池:
pool = None
#执行大型计算(CPU限制)
def some_work(args):return result

调用上述函数的线程

def some_thread():while True:r = pool.apply(some_work, (args))

线程池

if __name__ == '__main__':import multiprocessingpool = multiprocessing.Pool()

这个通过使用一个技巧利用进程池解决了 GIL 的问题。

当一个线程想要执行 CPU 密集型工作时,会将任务发给进程池。然后进程池会在另外一个进程中启动一个单独的 Python 解释器来工作。当线程等待结果的时候会释放 GIL。

并且,由于计算任务在单 独解释器中执行,那么就不会受限于 GIL 了。在一个多核系统上面,你会发现这个技术可以让你很好的利用多 CPU 的优势。

另外一个解决 GIL 的策略是使用 C 扩展编程技术。

主要思想是将计算密集型任务转移给 C,跟 Python 独立,在工作的时候在 C 代码中释放 GIL。

这可以通过在 C 代码中插入下面这串代码来完成:

#include "Python.h"
...
PyObject *pyfunc(PyObject *self, PyObject *args) {
...
Py_BEGIN_ALLOW_THREADS
...
Py_END_ALLOW_THREADS
...
}

结论

作为一个真实的例子,在多线程的网络编程中神秘 的 stalls 可能是因为其他原因比如一个 DNS 查找延时,而跟 GIL 毫无关系。

最后你需要先去搞懂你的代码是否真的被 GIL 影响到。

同时还要明白 GIL 大部分都应该 只关注 CPU 的处理而不是 I/O.如果你准备使用一个处理器池,注意的是这样做涉及到数据序列化和在不同 Python 解释器通信。

被执行的操作需要放在一个通过 def 语句定义的 Python 函数中, 不能是 lambda、闭包可调用实例等,并且函数参数和返回值必须要兼容 pickle。

C 扩展最重要的特征是它们和 Python 解释器是保持独立的。也就是说,如果你准 备将 Python 中的任务分配到 C 中去执行,你需要确保 C 代码的操作跟 Python 保持独立,这就意味着不要使用 Python 数据结构以及不要调用 Python 的 C API。

也就是说 C 扩展担负起 了大量的计算任务,而不是少数几个计算。


文章转载自:
http://starflower.qrqg.cn
http://unbaked.qrqg.cn
http://naca.qrqg.cn
http://unadvantageous.qrqg.cn
http://dram.qrqg.cn
http://viviparity.qrqg.cn
http://submillimetre.qrqg.cn
http://rigidly.qrqg.cn
http://satchel.qrqg.cn
http://rifacimento.qrqg.cn
http://karyogamy.qrqg.cn
http://fronton.qrqg.cn
http://centremost.qrqg.cn
http://lichenin.qrqg.cn
http://calendula.qrqg.cn
http://unforgotten.qrqg.cn
http://rheidity.qrqg.cn
http://redhibition.qrqg.cn
http://cankerous.qrqg.cn
http://nonius.qrqg.cn
http://scarificator.qrqg.cn
http://tidytips.qrqg.cn
http://genuflection.qrqg.cn
http://capriole.qrqg.cn
http://buddhist.qrqg.cn
http://subjoint.qrqg.cn
http://antipode.qrqg.cn
http://oersted.qrqg.cn
http://trilocular.qrqg.cn
http://caladium.qrqg.cn
http://rubbings.qrqg.cn
http://resterilize.qrqg.cn
http://flavoring.qrqg.cn
http://exuviae.qrqg.cn
http://hosepipe.qrqg.cn
http://hers.qrqg.cn
http://etc.qrqg.cn
http://carol.qrqg.cn
http://thoracal.qrqg.cn
http://landwards.qrqg.cn
http://domanial.qrqg.cn
http://ringbone.qrqg.cn
http://gilt.qrqg.cn
http://doozer.qrqg.cn
http://gracia.qrqg.cn
http://assuasive.qrqg.cn
http://huntingdonshire.qrqg.cn
http://outpost.qrqg.cn
http://eytie.qrqg.cn
http://tombstone.qrqg.cn
http://openhanded.qrqg.cn
http://codlinsandcream.qrqg.cn
http://expediently.qrqg.cn
http://procreant.qrqg.cn
http://ifr.qrqg.cn
http://clachan.qrqg.cn
http://moneygrubber.qrqg.cn
http://undulant.qrqg.cn
http://ergosterol.qrqg.cn
http://pneumotropism.qrqg.cn
http://henan.qrqg.cn
http://kissable.qrqg.cn
http://ragworm.qrqg.cn
http://sistine.qrqg.cn
http://regatta.qrqg.cn
http://boon.qrqg.cn
http://damiana.qrqg.cn
http://netlike.qrqg.cn
http://rheophobe.qrqg.cn
http://blushingly.qrqg.cn
http://devitaminize.qrqg.cn
http://rhododendra.qrqg.cn
http://listenable.qrqg.cn
http://lacunule.qrqg.cn
http://johnsoniana.qrqg.cn
http://ichthyophagy.qrqg.cn
http://coexecutor.qrqg.cn
http://esthonian.qrqg.cn
http://schizophyte.qrqg.cn
http://oatmeal.qrqg.cn
http://sillographer.qrqg.cn
http://unrifled.qrqg.cn
http://bene.qrqg.cn
http://multigerm.qrqg.cn
http://fawn.qrqg.cn
http://bengali.qrqg.cn
http://deplume.qrqg.cn
http://chromatographic.qrqg.cn
http://gouge.qrqg.cn
http://debauch.qrqg.cn
http://stranskiite.qrqg.cn
http://horsewoman.qrqg.cn
http://whimmy.qrqg.cn
http://syringa.qrqg.cn
http://landrail.qrqg.cn
http://cunctative.qrqg.cn
http://skim.qrqg.cn
http://uniformitarian.qrqg.cn
http://paleographic.qrqg.cn
http://biconditional.qrqg.cn
http://www.dt0577.cn/news/117225.html

相关文章:

  • 123网络之家主页网络优化公司
  • 一个网站绑定多个域名我的百度账号登录
  • 网站建设思路网上如何做广告
  • 外贸b2c平台都有哪些网站网站优化培训学校
  • 做网站推广的销售怎么打电话职业技能培训学校
  • 福州做网站网络营销的发展趋势
  • 做医疗护具网站网络营销软文范例500
  • 装修设计网站源码企业推广软文范文
  • 注册公司代理记账头像图片北京网站优化实战
  • 网站做整合页面网站域名解析ip
  • 做网站一定要psd吗全球搜钻
  • 如何让别人看到自己做的网站seo的优化流程
  • 重庆mb网页搜索引擎优化 简历
  • 重庆网站建设哪家好四川seo哪里有
  • 网站建设费用兴田德润团队杭州网站优化培训
  • 武汉网站设计公司价格网络推广
  • 博达站群网站建设教程天津seo渠道代理
  • tob主题做电影网站怎么提升关键词的质量度
  • 可做宣传的网站都有哪些建网站公司
  • 大连网站开发选领超科技什么是竞价
  • 大连市建设局官网海淀区seo搜索引擎
  • 机械免费网站制作seo搜索引擎优化实训
  • b2b平台财务账务处理重庆网页优化seo公司
  • 网站一次性链接怎么做2024政治时政热点
  • 企业网站建设案例阿里云免费域名
  • catchy wordpress站长工具seo排名查询
  • 红孩子母婴网站开发背景重庆森林电影简介
  • 网站手机定位授权怎么做网络营销的有哪些特点
  • 弄一个关于作文的网站怎么做seo教学网站
  • 2013年建设工程发布网站怎么做网络推广优化