Skip to main content
added 212 characters in body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k

The canonical version of this answer is now at the dupliquee question: What are the differences between the threading and multiprocessing modules?

The canonical version of this answer is now at the dupliquee question: What are the differences between the threading and multiprocessing modules?

added 218 characters in body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
 +--------------------------------------+ + Active threads / processes + +-----------+--------------------------------------+ |Thread 1 |******** ************ | | 2 | ***** *************| +-----------+--------------------------------------+ |Process 1 |*** ************** ****** **** | | 2 |** **** ****** ** ********* **********| +-----------+--------------------------------------+ + Time --> + +--------------------------------------+ 
+-----------+--------------------------------------+ |Thread 1 |******** ************ | | 2 | ***** *************| +-----------+--------------------------------------+ |Process 1 |*** ************** ****** **** | | 2 |** **** ****** ** ********* **********| +-----------+--------------------------------------+ 
 +--------------------------------------+ + Active threads / processes + +-----------+--------------------------------------+ |Thread 1 |******** ************ | | 2 | ***** *************| +-----------+--------------------------------------+ |Process 1 |*** ************** ****** **** | | 2 |** **** ****** ** ********* **********| +-----------+--------------------------------------+ + Time --> + +--------------------------------------+ 
added 970 characters in body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k

Visualize which threads are running at a given time

This post https://rohanvarma.me/GIL/ taught me that you can run a callback whenever a thread is scheduled with the target= argument of threading.Thread and the same for multiprocessing.Process.

This allows us to view exactly which thread runs at each time. When this is done, we would see something like (I made this particular graph up):

+-----------+--------------------------------------+ |Thread 1 |******** ************ | | 2 | ***** *************| +-----------+--------------------------------------+ |Process 1 |*** ************** ****** **** | | 2 |** **** ****** ** ********* **********| +-----------+--------------------------------------+ 

which would show that:

  • threads are fully serialized by the GIL
  • processes can run in parallel

Visualize which threads are running at a given time

This post https://rohanvarma.me/GIL/ taught me that you can run a callback whenever a thread is scheduled with the target= argument of threading.Thread and the same for multiprocessing.Process.

This allows us to view exactly which thread runs at each time. When this is done, we would see something like (I made this particular graph up):

+-----------+--------------------------------------+ |Thread 1 |******** ************ | | 2 | ***** *************| +-----------+--------------------------------------+ |Process 1 |*** ************** ****** **** | | 2 |** **** ****** ** ********* **********| +-----------+--------------------------------------+ 

which would show that:

  • threads are fully serialized by the GIL
  • processes can run in parallel
edited body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
Loading
edited body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
Loading
deleted 5 characters in body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
Loading
edited body
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
Loading
Source Link
Ciro Santilli OurBigBook.com
  • 392.6k
  • 120
  • 1.3k
  • 1.1k
Loading