0

I am developing a web app which has a service/task which might take a long time to finish. I am new to python and read that python has GIL, which means at one time only one thread can run irrespective of number of cores.

my pseudo code is like this

def service_xxx(self, data):
    thread = ThreadXXX(data)
    thread.start()
    self.threads[data.id] = thread

My questions is what happens when 100 requests comes ? Will flask framework run 100 user threads concurrently using all cores or its going to run 100 threads on single cores ?

wayfare
  • 1,802
  • 5
  • 20
  • 37

1 Answers1

0

Python (CPython) is not optimized for thread framework.You can keep allocating more resources and it will try spawning/queuing new threads and overloading the cores. You need to make a design change here:

Process based design:

  1. Either use the multiprocessing module
  2. Make use of rabbitmq and make this task run separately
  3. Spawn a subprocess

Or if you still want to stick to threads:

  1. Switch to PyPy (faster compared to CPython)
  2. Switch to PyPy-STM (totally does away with GIL)
Priyank Mehta
  • 2,453
  • 2
  • 21
  • 32