Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3616428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:29:52+00:00 2026-05-18T22:29:52+00:00

I have a Python program that spawns many threads, runs 4 at a time,

  • 0

I have a Python program that spawns many threads, runs 4 at a time, and each performs an expensive operation. Pseudocode:

for object in list:
    t = Thread(target=process, args=(object))
    # if fewer than 4 threads are currently running, t.start(). Otherwise, add t to queue

But when the program is run, Activity Monitor in OS X shows that 1 of the 4 logical cores is at 100% and the others are at nearly 0. Obviously I can’t force the OS to do anything but I’ve never had to pay attention to performance in multi-threaded code like this before so I was wondering if I’m just missing or misunderstanding something.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:29:52+00:00Added an answer on May 18, 2026 at 10:29 pm

    Note that in many cases (and virtually all cases where your "expensive operation" is a calculation implemented in Python), multiple threads will not actually run concurrently due to Python’s Global Interpreter Lock (GIL).

    The GIL is an interpreter-level lock.
    This lock prevents execution of
    multiple threads at once in the Python
    interpreter. Each thread that wants to
    run must wait for the GIL to be
    released by the other thread, which
    means your multi-threaded Python
    application is essentially single
    threaded, right? Yes. Not exactly.
    Sort of.

    CPython uses what’s called “operating
    system” threads under the covers,
    which is to say each time a request to
    make a new thread is made, the
    interpreter actually calls into the
    operating system’s libraries and
    kernel to generate a new thread. This
    is the same as Java, for example. So
    in memory you really do have multiple
    threads and normally the operating
    system controls which thread is
    scheduled to run. On a multiple
    processor machine, this means you
    could have many threads spread across
    multiple processors, all happily
    chugging away doing work.

    However, while CPython does use
    operating system threads (in theory
    allowing multiple threads to execute
    within the interpreter
    simultaneously), the interpreter also
    forces the GIL to be acquired by a
    thread before it can access the
    interpreter and stack and can modify
    Python objects in memory all
    willy-nilly. The latter point is why
    the GIL exists: The GIL prevents
    simultaneous access to Python objects
    by multiple threads. But this does not
    save you (as illustrated by the Bank
    example) from being a lock-sensitive
    creature; you don’t get a free ride.
    The GIL is there to protect the
    interpreters memory, not your sanity.

    See the Global Interpreter Lock section of Jesse Noller’s post for more details.

    To get around this problem, check out Python’s multiprocessing module.

    multiple processes (with judicious use
    of IPC) are[…] a much better
    approach to writing apps for multi-CPU
    boxes than threads.

    — Guido van Rossum (creator of Python)

    Edit based on a comment from @spinkus:

    If Python can’t run multiple threads simultaneously, then why have threading at all?

    Threads can still be very useful in Python when doing simultaneous operations that do not need to modify the interpreter’s state. This includes many (most?) long-running function calls that are not in-Python calculations, such as I/O (file access or network requests)) and [calculations on Numpy arrays][6]. These operations release the GIL while waiting for a result, allowing the program to continue executing. Then, once the result is received, the thread must re-acquire the GIL in order to use that result in "Python-land"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that performs several independent and time consuming processes. The
I have a Python program that runs a series of experiments, with no data
I have a java program that runs another (Python) program as a process. Process
I have a Python 3.2 program that runs like this: import platform sysname =
I have a python program that is running for many days. Memory usage does
I have a Python program that runs a cell model continuously. when I press
I have a Python program that executes a large MySQL statement. How do I
I have a python program that takes the md5 & sha1 hash values of
I have a python program that must work on Windows and Linux. There are
I have a python program that opens several urls in seperate tabs in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.