Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Limits . . .
#1
On a computer with only (1 CPU), how many threads can I run at the same time in the python process ?
Reply
#2
Threads have nothing to do with cpus
Reply
#3
(Mar-01-2023, 02:12 AM)deanhystad Wrote: Threads have nothing to do with cpus

Of course it is, a thread is the smallest processing unit, it's what the CPU does.
Reply
#4
In pure python, you can have multiple threads active at one time, but only 1 will be running at any one time (regardless of how many cpus you have).
JohnnyCoffee likes this post
Reply
#5
The concept to understand is the GIL. Here is an article that explains what the GIL is, why multiple threads may exist and yet only one executes at a time, etc.

It comes down to that Python uses reference counting for variables rather than garbage collection.
Reply
#6
(Mar-01-2023, 01:09 PM)jefsummers Wrote: The concept to understand is the GIL. Here is an article that explains what the GIL is, why multiple threads may exist and yet only one executes at a time, etc.

It comes down to that Python uses reference counting for variables rather than garbage collection.

And if I use Cython, to program simultaneous multithreading, will I be able to escape the GIL?
Reply
#7
You likely won't see any problems that are related to the global interpreter lock. I've done quite a bit of programming involving muliple threads talking to different processes (IO intensive) and have never had a problem that I think is related to GIL.

What are you using threads for that makes you think you'll have huge numbers of threads.
Reply
#8
(Mar-01-2023, 09:49 PM)deanhystad Wrote: You likely won't see any problems that are related to the global interpreter lock. I've done quite a bit of programming involving muliple threads talking to different processes (IO intensive) and have never had a problem that I think is related to GIL.

What are you using threads for that makes you think you'll have huge numbers of threads.

I'm developing a very performant http web server.
Reply
#9
In Cython you can release the GIL, but given the application (will be IO intensive rather than CPU intensive) I would agree with @deanhystad that the GIL is unlikely to get in your way. And, releasing the GIL means you have to do the GIL's work. There is no free lunch.

IMHO, your bottlenecks will be getting info from your long term storage and placing that info on the network, not CPU processing. Threads will be "happy" to yield the processor while waiting for those things to happen.

I don't know the details, but a quick Google search tells me that the following popular sites are done in Python:
Youtube
Instagram
Reddit
Spotify
and the list goes on
Reply
#10
(Mar-02-2023, 12:57 PM)jefsummers Wrote: In Cython you can release the GIL, but given the application (will be IO intensive rather than CPU intensive) I would agree with @deanhystad that the GIL is unlikely to get in your way. And, releasing the GIL means you have to do the GIL's work. There is no free lunch.

IMHO, your bottlenecks will be getting info from your long term storage and placing that info on the network, not CPU processing. Threads will be "happy" to yield the processor while waiting for those things to happen.

I don't know the details, but a quick Google search tells me that the following popular sites are done in Python:
Youtube
Instagram
Reddit
Spotify
and the list goes on

I am studying the operation of the processor in a very critical way to find out how I can make a possibly simultaneous implementation in the case of 1 Processor with only 1 Core using ( C ) or Cython itself.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do i add limits to this code ? learn1 3 2,046 Feb-07-2020, 02:30 AM
Last Post: stullis
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,606 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020