I had to do heavy I/o bound operation, i.e Parsing large files and converting from one format to other format. Initially I used to do it serially, i.e parsing one after another..! Performance was very poor ( it used take 90+ seconds). So I decided to use threading to improve the performance. I created one thread for each file. ( 4 threads)
for file in file_list:
t=threading.Thread(target = self.convertfile,args = file)
t.start()
ts.append(t)
for t in ts:
t.join()
But for my astonishment, there is no performance improvement whatsoever. Now also it takes around 90+ seconds to complete the task. As this is I/o bound operation , I had expected to improve the performance.
Under the usual Python interpreter, threading will not allocate more CPU cores to your program because of the global interpreter lock (aka. the GIL).
The multiprocessing module could help you out here. (Note that it was introduced in Python 2.6, but backports exist for Python 2.5.)
As MSalters says, if your program is I/O bound it’s debatable whether this is useful. But it might be worth a shot 🙂
To achieve what you want using this module:
Important! The function that you pass to
map_asyncmust be pickleable. In general, instance methods are NOT pickleable unless you engineering them to be so! Note thatconvertfileabove is a function.If you actually need to get results back from
convertfile, there are ways to do that as well. The examples on the multiprocessing documentation page should clarify.