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

  • Home
  • SEARCH
  • 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 7799999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:29:25+00:00 2026-06-02T00:29:25+00:00

I am attempting to have two long running operations run simultaneously in python. They

  • 0

I am attempting to have two long running operations run simultaneously in python. They both operate on the same data set, but do not modify it. I have found that a threaded implementation runs slower than simply running them one after the other.

I have created a simplified example to show what I am experiencing.

Running this code, and commenting line 46 (causing it to perform the operation threaded), results in a runtime on my machine of around 1:01 (minute:seconds). I see two CPUs run at around 50% for the full run time.

Commenting out line 47 (causing sequential calculations) results in a runtime of around 35 seconds, with 1 CPU being pegged at 100% for the full runtime.
Both runs result in the both full calculations being completed.

from datetime import datetime
import threading


class num:
    def __init__(self):
        self._num = 0

    def increment(self):
        self._num += 1

    def getValue(self):
        return self._num

class incrementNumber(threading.Thread):
    def __init__(self, number):
        self._number = number
        threading.Thread.__init__(self)

    def run(self):
        self.incrementProcess()

    def incrementProcess(self):
        for i in range(50000000):
            self._number.increment()


def runThreaded(x, y):
    x.start()
    y.start()
    x.join()
    y.join()

def runNonThreaded(x, y):
    x.incrementProcess()
    y.incrementProcess()

def main():
    t = datetime.now()

    x = num()
    y = num()
    incrementX = incrementNumber(x)
    incrementY = incrementNumber(y)

    runThreaded(incrementX, incrementY)
    #runNonThreaded(incrementX, incrementY)


    print x.getValue(), y.getValue()
    print datetime.now() - t


if __name__=="__main__":
    main()
  • 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-02T00:29:27+00:00Added an answer on June 2, 2026 at 12:29 am

    CPython has a so-called Global Interpreter Lock, which means that only one Python statement can run at a time even when multithreading. You might want to look into multiprocessing, which avoids this constraint.

    The GIL means that Python multithreading is only useful for I/O-bound operations, other things that wait for stuff to happen, or if you’re calling a C extension that releases the GIL while doing work.

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

Sidebar

Related Questions

I am attempting to profile a long running python script. The script does some
I have two large data sets and I am attempting to reformat the older
I have a simple in VB/ASP.NET form containing two text boxes, I am attempting
I have two tables with the exact same schema in SQL Server 2008 Standard
This is driving me nuts. I have two tables that I am attempting to
I have two questions about undefined method `distance_of_time_in_words' In my view I am attempting
I am attempting to do an UPDATE with a JOIN. I have two tables:
We have two pieces of architecture. In essence they form a producer and a
I have two variables, $_REQUEST['amount'] and $carttotal , on an e-commerce thing. They of
I have two separate projects, a plus and free version. Both projects use 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.