-1

I learn that in cPython interpreter there is a GIL which causes the OS running one thread at a moment even if we have a multi-core processor. Meanwhile, changing from one thread to another brings a little time cost. So I wonder in multitask mode can we use more threads to improve efficiency?

  • Possible duplicate of [this thread](https://stackoverflow.com/questions/20939299/does-python-support-multithreading-can-it-speed-up-execution-time?rq=1) – Gribouillis Aug 13 '17 at 07:14

1 Answers1

0

For use more than one core in your Python program, you must use concurrency. For example:

import threading

def methodA():
   #Do Something 
def methodB():
  #Do Something
aThread = threading.Thread(target=methodA)
aThread.start()
bThread= threading.Thread(target=methodA)
bThread.start()

The aThread will execute the methodA when you put de .start(), the same as de bThread but with the methodB, at the same time in different cores.

If you need to wait for the two thread until they finish their work use .join() like this:

aThread.join()
bThread.join()
#Then continue.

The concurrency it´s a very big world with a lot of options and methods to use in different cases.

I really recommend to visit the official site.

F.Stan
  • 553
  • 1
  • 10
  • 23