Python is said to be a language that is not suitable for multithreading. That's because python uses the 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 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