Multithreaded processing in python

Python is not suitable for multithreaded processing

Python is said to be a language that is not suitable for multithreading. That's because python uses the GIL.

What is GIL

GIL:Global Interpreter lock A mechanism to ensure that bytecode is executed by only one thread at a time. This is adopted in python, so basically multithreading is not possible.

--Disadvantages of GIL Limited to one thread that can run concurrently

--Advantages of GIL Speed up single-threaded programs

Is it better to use process-based parallel processing because it uses GIL? It seems that.

Some modules release the GIL

Some python extension modules are designed to free the GIL when performing compression or hashing computationally intensive tasks.

Example

multithread.py



import threading, zipfile

#Class to compress files.GIL is released
class AsyncZip(threading.Thread):
    def __init__(self, infile, outfile):
        threading.Thread.__init__(self)
        self.infile = infile
        self.outfile = outfile
​
    def run(self):
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) #Normal compression
        f.write(self.infile)
        f.close()
        print('background zip compress process finished : ', self.infile)
​
# mydata.txt is prepared appropriately
background = AsyncZip('mydata.txt', 'myarchive.zip')

#Confirm that the main program is alive at the same time even if background processing is executed
background.start()
print('main program still working')
​
#Make sure the main program is alive until the background processing is finished
background.join()
print('main program was waiting for a background thread')

Execution result

C:\Users\NAME\.PyCharmCE2019.2\system\workspace>python3 multithread.py
main program still working
background zip compress process finished: mydata.txt
main program was waiting for a background thread

Recommended Posts

Multithreaded processing in python
File processing in Python
Text processing in Python
Queue processing in Python
UTF8 text processing in python
Asynchronous processing (threading) in python
Image Processing Collection in Python
Using Python mode in Processing
Signal processing in Python (1): Fourier transform
100 Language Processing Knock Chapter 1 in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python image processing
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python file processing
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Easy image processing in Python with Pillow
Duplicate prohibition processing in GAE / Python Datastore
Status of each Python processing system in 2020
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python