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 8153289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:58:08+00:00 2026-06-06T15:58:08+00:00

I’ve first observed this issue in a production code, then made a prototype: import

  • 0

I’ve first observed this issue in a production code, then made a prototype:

import threading, Queue, time, sys

def heavyfunc():
    ''' The idea is just to load CPU '''
    sm = 0
    for i in range(5000):
        for j in range(5000):
            if i + j % 2 == 0:
                sm += i - j
    print "sm = %d" % sm

def worker(queue):
    ''' worker thread '''
    while True:
        elem = queue.get()
        if elem == None: break
        heavyfunc()           # whatever the elem is


starttime = time.time()  

q = Queue.Queue()             # queue with tasks

number_of_threads = 1
# create & start number_of_threads working threads
threads = [threading.Thread(target=worker, args=[q]) for thread_idx in range(number_of_threads)]
for t in threads: t.start()

# add 2 working items: they are estimated to be computed in parallel
for x in range(2):
    q.put(1)

for t in threads: q.put(None) # Add 2 'None' => each worker will exit when gets them
for t in threads: t.join()    # Wait for every worker

#heavyfunc()

elapsed = time.time() - starttime

print >> sys.stderr, elapsed

The idea of heavyfunc() is just to load CPU, without any synchronization and dependencies.

When using 1 thread, it takes 4.14 sec in average
When using 2 threads, it takes 6.40 sec in average
When not using any threads, to compute heavyfunc() takes 2.07 sec in average (measured many times, that’s exactly 4.14 / 2, as in case with 1 thread and 2 tasks).

I’m expecting 2 jobs with heavyfunc() to take 2.07 sec, provided there are 2 threads. (My CPU is i7 => there are enough cores).

Here is the CPU monitor’s screenshots that also give the idea there were no true multithreading:

CPU Load Graphi

Where is the error in my thinking? How do I create n threads that don’t interfere?

  • 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-06-06T15:58:10+00:00Added an answer on June 6, 2026 at 3:58 pm

    CPython will not execute bytecode on more than one core at once. Multi-threading cpu-bound code is pointless. The Global Interpreter Lock (GIL) is there to protect all of the reference counts in the process, so only one thread can use Python objects at a time.

    You are seeing worse performance because you still only have one thread at a time working, but now you are also changing thread contexts.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker

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.