gil

How can I check whether a thread currently holds the GIL?

谁说胖子不能爱 提交于 2019-11-29 17:53:08
问题 I tried to find a function that tells me whether the current thread has the global interpreter lock or not. The Python/C-API documentation does not seem to contain such a function. My current solution is to just acquire the lock using PyGILState_Ensure() before releasing it using PyEval_SaveThread to not try releasing a lock that wasn't acquired by the current thread. (btw. what does "issues a fatal error" mean?) Background of this question: I have a multithreaded application which embeds

Understanding python GIL - I/O bound vs CPU bound

半城伤御伤魂 提交于 2019-11-29 12:38:58
From python threading documentation In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously. Now I have a thread worker like this def worker(queue): queue_full = True while queue_full: try: url = queue.get(False) w =

A question on python GIL

谁都会走 提交于 2019-11-29 11:42:46
Does the presence of python GIL imply that in python multi threading the same operation is not so different from repeating it in a single thread?. For example, If I need to upload two files, what is the advantage of doing them in two threads instead of uploading them one after another?. I tried a big math operation in both ways. But they seem to take almost equal time to complete. This seems to be unclear to me. Can someone help me on this?. Thanks. Python's threads get a slightly worse rap than they deserve. There are three (well, 2.5) cases where they actually get you benefits: If non-Python

关于GIL一点思考

泄露秘密 提交于 2019-11-29 08:31:45
什么是GIL? GIL全称是Global Interpreter Lock,是python最常见的解释器CPython引入的一个概念。GIL是为了避免多个线程(threads)同时执行。因为CPython的内存管理并不是线程安全的,所以这个锁的存在是有必要的,短时间也是无法移除的。 GIL是一把全局排他所。毫无疑问,全局锁的存在会多线程的效率有不小影响。甚至就几乎等于Python是个单线程的程序。 影响 GIL最大的问题就是Python的多线程程序并不能利用多核CPU的优势。 GIL只会影响到那些严重依赖CPU的程序(计算型),如果你 如何避免影响 用multiprocess替代Thread 即使用多进程而不是多线程。每个进程有自己的独立的GIL,因此不会出现进程之间的GIL争抢。 缺点:线程之间共享变量比较容易,对于thread来说,申明一个global变量,用thread.Lock的context包裹住三行就搞定了。而multiprocess由于进程之间无法看到对方的数据,只能通过在主线程申明一个Queue,put再get或者用share memory的方法。 使用其他解释器 JPython和IronPython没有GIL的问题,但是比较小众。功能和性能较差,不是个好选择。 核心部分使用其他语言 如果对并行计算性能较高的程序可以考虑把核心部分写成C模块,或者索性用其他语言实现

Why does this Python script run 4x slower on multiple cores than on a single core

别说谁变了你拦得住时间么 提交于 2019-11-29 07:03:48
I'm trying to understand how CPython's GIL works and what are the differences between GIL in CPython 2.7.x and CPython 3.4.x. I'm using this code for benchmarking: from __future__ import print_function import argparse import resource import sys import threading import time def countdown(n): while n > 0: n -= 1 def get_time(): stats = resource.getrusage(resource.RUSAGE_SELF) total_cpu_time = stats.ru_utime + stats.ru_stime return time.time(), total_cpu_time, stats.ru_utime, stats.ru_stime def get_time_diff(start_time, end_time): return tuple((end-start) for start, end in zip(start_time, end

Python GIL锁

折月煮酒 提交于 2019-11-29 06:00:49
Cpython进程与其运行文件所产生的主进程是一个进程(文件进程相当于Cpython的一个线程)线程的特点是数据资源是共享的,而多个线程又都要共享Cpython的解释权限,共享意味着竞争,有竞争数据就不安全,所以Cpython的GIL锁(Cpython的一个线程)就产生了,根本作用是,当python文件中的线程想要执行其代码,必须获得GIL权限,否则不能执行,所以cpu的多核优势也没有了,除非多开Cpython解释器或多进程,否则同时只能运行一个线程 一个工人相当于cpu,此时计算相当于工人在干活,I/O阻塞相当于为工人干活提供所需原材料的过程,工人干活的过程中如果没有原材料了,则工人干活的过程需要停止,直到等待原材料的到来。 如果你的工厂干的大多数任务都要有准备原材料的过程(I/O密集型),那么你有再多的工人,意义也不大,还不如一个人,在等材料的过程中让工人去干别的活, 反过来讲,如果你的工厂原材料都齐全,那当然是工人越多,效率越高 结论:   对计算来说,cpu越多越好,但是对于I/O来说,再多的cpu也没用   当然对运行一个程序来说,随着cpu的增多执行效率肯定会有所提高(不管提高幅度多大,总会有所提高),这是因为一个程序基本上不会是纯计算或者纯I/O,所以我们只能相对的去看一个程序到底是计算密集型还是I/O密集型,从而进一步分析python的多线程到底有无用武之地。

GIL(全局解释器锁)

佐手、 提交于 2019-11-29 05:04:46
来一道GIL面试题   描述python GIL的概念,以及他对python多线程的影响,编写一个多线程抓取网页的程序,并阐明多线程抓取程序是否比单线程有所提升,并解释原因。   参考答案:        1.python语言和GIL没有关系,仅仅是由于历史原因在CPython虚拟机(解释器),难以移除GIL       2.GIL:全局解释器锁,每个线程在执行的过程都需要先获取GIL,保证同一时刻只有一个线程可以执行代码       3.线程是放GIL的情况:在IO操作等可能会引起阻塞的system call之前,可以暂时释放GIL,但在执行完毕后必须重新获取GIL       4.python使用多进程是可以利用多核的资源的       5.多线程爬取比单线程有提升,因为遇到IO阻塞会自动释放GIL锁 为python喊冤   GIL全局解释锁与python没有一点的关系,只是跟cPython解释有关,那什么由解释器呢?我们都知道,机器只认得0,1这样组成的数字,而我们写的代码却不是,那么我们就就需要将我们写的代码转化为机器认识的0,1,那么解释器就是干了这么一件事。然而解释可以用c,java,c#等其他语言写,但python官网推荐是c写的解释器,人们就叫他cpython.java也写过一个解释器叫Jpython.而cpython解释器中有GIL,Jpython中没有。

Is concurrent.futures a medicine of the GIL?

 ̄綄美尐妖づ 提交于 2019-11-29 02:52:20
问题 I was just searching about this new implementation, and i use python 2.7, i must install this, so if i use it, i'll forget the word GIL on CPython? 回答1: No, concurrent.futures has almost nothing whatsoever to do with the GIL. Using processes instead of threads is medicine for the GIL. (Of course, like all medicine, it has side effects. But it works.) The futures module just gives you a simpler way to schedule and wait on tasks than using threading or multiprocessing directly. And it has the

Embedding python in multithreaded C application

依然范特西╮ 提交于 2019-11-29 01:24:25
I'm embedding the python interpreter in a multithreaded C application and I'm a little confused as to what APIs I should use to ensure thread safety. From what I gathered, when embedding python it is up to the embedder to take care of the GIL lock before calling any other Python C API call. This is done with these functions: gstate = PyGILState_Ensure(); // do some python api calls, run python scripts PyGILState_Release(gstate); But this alone doesn't seem to be enough. I still got random crashes since it doesn't seem to provide mutual exclusion for the Python APIs. After reading some more

numpy and Global Interpreter Lock

☆樱花仙子☆ 提交于 2019-11-29 01:13:48
I am about to write some computationally-intensive Python code that'll almost certainly spend most of its time inside numpy 's linear algebra functions. The problem at hand is embarrassingly parallel . Long story short, the easiest way for me to take advantage of that would be by using multiple threads. The main barrier is almost certainly going to be the Global Interpreter Lock (GIL). To help design this, it would be useful to have a mental model for which numpy operations can be expected to release the GIL for their duration. To this end, I'd appreciate any rules of thumb, dos and don'ts,