+ 4

Is it a good idea to use multi-thread to speed our Python code?

18th Jun 2021, 4:04 AM
RuBeen Dahal👿
4 ответов
+ 2
Multiprocesses if CPU is the bottleneck Multithread if i/o is bottleneck. For CPU bottlenecks multithreading will be useless. Read more about this in Google tags=python gil https://realpython.com/python-gil/
18th Jun 2021, 8:49 AM
Oma Falk
Oma Falk - avatar
+ 1
There is potential to get a speed increase if the threads get executed in parallel across multiple CPU cores or if hyperthreading is supported in the CPU. Also multithreading can improve speed if a thread performs i/o and gets blocked while waiting for the i/o device. At that point another thread may be given execution time while the blocked thread is waiting.
18th Jun 2021, 6:04 AM
Brian
Brian - avatar
0
So, in principle, if you want multi-threaded code to speed up computations, it won't speed it up since only one thread is executing at a time, but if you use it, for example, to interact with a database, it will work.
18th Jun 2021, 4:32 AM
SammE
SammE - avatar
0
This code has some scanning methods both with and without threads. The threaded methods work at least 3 times faster than the non threaded. if you want to test, just add a reference to a nonthreaded method in the command dictionary. https://code.sololearn.com/ce1VUemhhYwS/?ref=app
18th Jun 2021, 8:44 AM
Slick
Slick - avatar