Python parallel / parallel processing sample code summary

I will show you how to do two or more processes at the same time in Python

--Thread --Thread pool --Process pool --Event loop (coroutine)

Threading

Threads allow you to run multiple functions at the same time. It works if you pass the function as target to the threading.Thread class and start it with start ().

import time
import threading


def func1():
    while True:
        print("func1")
        time.sleep(1)


def func2():
    while True:
        print("func2")
        time.sleep(1)


if __name__ == "__main__":
    thread_1 = threading.Thread(target=func1)
    thread_2 = threading.Thread(target=func2)

    thread_1.start()
    thread_2.start()

Execution result

func1
func2
func2
func1
func1
func2
func2
func1

Thread pool (concurrent.futures)

It is even more powerful if you use the concurrent.futures package of Python 3.2 or later. Use the ThreadPoolExecutor class in it.

If you decide the maximum number max_workers to run at the same time first, it will use threads again, so it is smarter than the ordinary threads introduced above. If you have a newer version of Python, you can actively use it.

import time
import concurrent.futures


def func1():
    while True:
        print("func1")
        time.sleep(1)


def func2():
    while True:
        print("func2")
        time.sleep(1)


if __name__ == "__main__":
    executor = concurrent.futures.ThreadPoolExecutor(max_workers=2)
    executor.submit(func1)
    executor.submit(func2)

Execution result

func1
func2
func1
func2
func1
func2
func1
func2

Process pool (concurrent.futures)

Same as above concurrent.futures package, but it's called ** process pool ** instead of thread pool There is also.

By dividing into process units instead of threads, you will not be subject to Global Interpreter Lock (GIL) restrictions ** You will be able to operate with multiple cores. ** ** However, since it uses a process that is larger than the thread, other restrictions may increase. Caution!

It's easy to use, just change the ThreadPoolExecutor introduced above to ProcessPoolExecutor.

import time
import concurrent.futures


def func1():
    while True:
        print("func1")
        time.sleep(1)


def func2():
    while True:
        print("func2")
        time.sleep(1)


if __name__ == "__main__":
    executor = concurrent.futures.ProcessPoolExecutor(max_workers=2)
    executor.submit(func1)
    executor.submit(func2)

Execution result

func1
func2
func1
func2
func1
func2
func1
func2

Event loop (coroutine)

There is also a way to run multiple processes in one thread. One of them is the ** event loop **. In Python3.4 or later, it can be realized with asyncio module.

It is easy to understand the difference from multithreading and when to use it ... by reading Asynchronous processing in Python: asyncio reverse lookup reference. ..

It is much more efficient than increasing threads for asynchronous I / O such as communication and file input / output, but it is difficult to get used to because the concept is difficult.

The sample code is quite different from the thread case.

I'm using ʻasyncio.sleepinstead oftime.sleep to wait, because I call ʻasyncio.sleep and move to another parallel process while waiting. It's just a coroutine.

import asyncio


@asyncio.coroutine
def func1():
    while True:
        print("func1")
        yield from asyncio.sleep(1)


@asyncio.coroutine
def func2():
    while True:
        print("func2")
        yield from asyncio.sleep(1)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    tasks = asyncio.wait([func1(), func2()])
    loop.run_until_complete(tasks)

Execution result

func2
func1
func2
func1
func2
func1
func2
func1

Recommended Posts

Python parallel / parallel processing sample code summary
[Python] Sample code for Python grammar
Python Summary
Python parallel processing (multiprocessing and Joblib)
Python summary
Ported Python parallel computing sample to F #
python image processing
Python tutorial summary
Python closure sample
python character code
Python file processing
[Python] Algorithm-aware code
python related summary
Python basics summary
How to do multi-core parallel processing with python
Parallel processing with no deep meaning in Python
Python distributed processing Spartan
Summary about Python scraping
[Language processing 100 knocks 2020] Summary of answer examples by Python
Python Django tutorial summary
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
Specific sample code for working with SQLite3 in Python
Python code acceleration approach
Machine learning python code summary (updated from time to time)
Rewrite Python2 code to Python3 (2to3)
First Python image processing
Parallel download in Python
infomap python draw code
Before writing Python code
Summary about Python3 + OpenCV3
An introduction to Python distributed parallel processing with Ray
Ajax + Python + PostgreSQL sample
Python function argument summary
Text processing in Python
About Python3 character code
Queue processing in Python
Sample code to get started with GLSL shaders in Processing (either Java or Python)
Python directory operation summary
Python AI framework summary
Python iteration related summary
Image processing with Python
Parallel processing with multiprocessing
Summary of Python arguments
[Memo] Test code summary
Python Requests status code
OpenCV basic code (python)
Python string processing illustration
Summary of date processing in Python (datetime and dateutil)
Various processing of Python
Python --Simple multi-thread sample
[Python] Data Science 100 Knock (Structured Data Processing) 001-010 Impressions + Explanation Link Summary
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
I just wrote the original material for the python sample code
Sample code summary when working with Google Spreadsheets from Google Colab
Send push notifications to iOS apps with Python2 (with sample code)
Get country code with python
Image processing with Python (Part 2)
Sample data created with python