Use the process pool to circumvent Python GIL restrictions, and the pool to circumvent pythongil

Source: Internet
Author: User

Use the process pool to circumvent Python GIL restrictions, and the pool to circumvent pythongil

Operating System: CentOS7.3.1611 _ x64

Python version: 2.7.5

Problem description

Python GIL will affect CPU-intensive programs. If Python is fully used for programming, how can we avoid GIL restrictions?

Solution

Use a process pool in multiple threads to avoid GIL restrictions. The details are as follows:

1. Use the multiprocessing module to create a process pool;

2. Assign computing tasks to different threads;

3. Submit the task to the previously created process pool in the task thread;

 

Whenever a thread needs to execute a cpu-intensive task, the task is submitted to the process pool, and the process pool will hand over the task to the Python interpreter running in another process.

When the thread waits for the result, GIL is released, and the computing task is executed in another separate Python interpreter, which is no longer limited by GIL.

Using this solution in a multi-core system makes it easy to use all the CPU cores.

 

Suppose there is such a worker function:

def worker(arr):    s = 0    for n in arr :        arrTmp = range(1,n+1)        if len(arrTmp) == 0 : continue        rtmp = 1        for i in arrTmp :            rtmp *= i        s += rtmp    return s

Complete code: https://github.com/mike-zhang/pyExamples/blob/master/gilAvoid/gilAvoidTest1/taskCommon.py

Common Single-Process Task implementation:

def main():    s = 0    tStart,tStop = 1,1000    for i in range(1,100):        #t = worker(range(tStart,tStop))        t = worker(range(1,1000))        s += t        tStart = tStop        tStop += 1000    print("len : ",len(str(s)))    print(s%10000)

Complete code: https://github.com/mike-zhang/pyExamples/blob/master/gilAvoid/gilAvoidTest1/t1_normal.py

The running effect is as follows:

(py27env) [mike@localhost test]$ time python t1_normal.py('len : ', 2567)987real    0m17.919suser    0m17.915ssys     0m0.003s

Use process pool implementation:

def wokerThread(start,stop):    #r = gPool.apply(worker,(range(start,stop),))    r = gPool.apply(worker,(range(1,1000),))    q.put(r)def main():    s = 0    thrdArr = []    tStart,tStop = 1,1000    for i in range(1,gCount+1):        thrd = threading.Thread(target=wokerThread,args=(tStart,tStop))        thrdArr.append(thrd)        tStart = tStop        tStop += 1000    for t in thrdArr :        t.daemon = True        t.start()    while not q.full(): time.sleep(0.1)    while not q.empty(): s += q.get()    print("len : ",len(str(s)))    print(s%10000)

Complete code: https://github.com/mike-zhang/pyExamples/blob/master/gilAvoid/gilAvoidTest1/t2_mp.py

The running effect is as follows:

(py27env) [mike@localhost test]$ time python t2_mp.pyqueue full('len : ', 2567)987real    0m4.917suser    0m18.356ssys     0m0.146s

We can see that the above method can avoid GIL restrictions (the test machine is 4-core i5), and the program speed is significantly improved.

Okay, that's all. I hope it will help you.

Github address:

Bytes

Please add

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.