shammery
shammery

Reputation: 1072

Optimal number of threads to use in Python threaing

I want to call an API to get some data in response via a socket, so let's say I have 58 threads which call the APIs in parallel. Ideally, I wanted them to run all of them at once and does not put a load on my machine. But for some reason, some threads are blocked, I assume that there are is a limitation on threads to call based on your system.

admin:~$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               58
Model name:          Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Stepping:            9
CPU MHz:             1434.299
CPU max MHz:         3100.0000
CPU min MHz:         1200.0000
BogoMIPS:            4988.39
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            3072K
NUMA node0 CPU(s):   0-3

There are two things, I want to know about:

  • How many threads I can create which does not block, or an optimum number of threads to call?
  • If I can't run those 58 threads, is there a better way?
  • If I replace multiprocessing with multithreading, will that help?

UPDATE 1

After reading some valueable comments, I figured out that my question is not clear enough, so here I will try to be clear this time around.

I have 58 different apis to hit for data, let's say for the assumption, we have a server where there are cameras installed(58 cams). I want to call them individually to get frames from every camera and I want them at once. For instance, to get all camera frame at a given timestamp, I send an HTTP request and get the image. Problem is when i run the 58 threads, some threads works well, while some thread takes alot of time, Its my understanding that these threads are blocking. How do I ensure that every call takes ~0.5 sec to get a frame (assume that it takes ~5 sec to get one frame)

Language: Python3

Would someone help me understand and optimize this process? Thanks

Upvotes: 2

Views: 4975

Answers (1)

StuxCrystal
StuxCrystal

Reputation: 906

You want to use AsyncIO.


The standard Python implementation has a GIL. This means only one thread can run at a time. This means that you don't want any additional threads if you can help it.

Over the years, python came up with multiple solutions around the GIL.

One of them is multiprocessing the other one is asyncio. Since you are waiting for (what I assume Network) API responses you should take a look at AsyncIO.

  • Multiprocessing spawns new processes. If your program is doing heavy computations, use the Multiprocessing module to paralellize your workload.
  • If your threads are primarily waiting for IO, AsyncIO is the direction you should look at.

Upvotes: 4

Related Questions