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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:36:00+00:00 2026-05-27T08:36:00+00:00

I was developing an app on gae using python 2.7, an ajax call requests

  • 0

I was developing an app on gae using python 2.7, an ajax call requests some data from an API, a single request could take ~200 ms, however when I open two browsers and make two requests at a very close time they take more than the double of that, I’ve tried putting everything in threads but it didn’t work.. (this happens when the app is online, not just on the dev-server)

So I wrote this simple test to see if this is a problem in python in general (in case of a busy wait), here is the code and the result:

def work():
    t = datetime.now()
    print threading.currentThread(), t
    i = 0
    while i < 100000000:
        i+=1
    t2 = datetime.now()
    print threading.currentThread(), t2, t2-t

if __name__ == '__main__': 
    print "single threaded:"
    t1 = threading.Thread(target=work)
    t1.start()
    t1.join()

    print "multi threaded:"
    t1 = threading.Thread(target=work)
    t1.start()
    t2 = threading.Thread(target=work)
    t2.start()
    t1.join()
    t2.join()

The result on mac os x, core i7 (4 cores, 8 threads), python2.7:

single threaded:
<Thread(Thread-1, started 4315942912)> 2011-12-06 15:38:07.763146
<Thread(Thread-1, started 4315942912)> 2011-12-06 15:38:13.091614 0:00:05.328468

multi threaded:
<Thread(Thread-2, started 4315942912)> 2011-12-06 15:38:13.091952
<Thread(Thread-3, started 4323282944)> 2011-12-06 15:38:13.102250
<Thread(Thread-3, started 4323282944)> 2011-12-06 15:38:29.221050 0:00:16.118800
<Thread(Thread-2, started 4315942912)> 2011-12-06 15:38:29.237512 0:00:16.145560

This is pretty shocking!! if a single thread would take 5 seconds to do this.. I thought starting two threads at the same time will take the same time to finish both tasks, but it takes almost triple the time.. this makes the whole threading idea useless, as it would be faster to do them sequentially!

what am I missing here..

  • 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-27T08:36:00+00:00Added an answer on May 27, 2026 at 8:36 am

    David Beazley gave a talk about this issue at PyCon 2010.
    As others have already stated, for some tasks, using threading especially with multiple cores can lead to slower performance than the same task performed by a single thread. The problem, Beazley found, had to do with multiple cores having a “GIL battle”:

    enter image description here

    To avoid GIL contention, you may get better results having the tasks run in separate processes instead of separate threads. The multiprocessing module provides a convenient way to do that especially since multiprocessing API is very similar to the threading API.

    import multiprocessing as mp
    import datetime as dt
    def work():
        t = dt.datetime.now()
        print mp.current_process().name, t
        i = 0
        while i < 100000000:
            i+=1
        t2 = dt.datetime.now()
        print mp.current_process().name, t2, t2-t
    
    if __name__ == '__main__': 
        print "single process:"
        t1 = mp.Process(target=work)
        t1.start()
        t1.join()
    
        print "multi process:"
        t1 = mp.Process(target=work)
        t1.start()
        t2 = mp.Process(target=work)
        t2.start()
        t1.join()
        t2.join()
    

    yields

    single process:
    Process-1 2011-12-06 12:34:20.611526
    Process-1 2011-12-06 12:34:28.494831 0:00:07.883305
    multi process:
    Process-3 2011-12-06 12:34:28.497895
    Process-2 2011-12-06 12:34:28.503433
    Process-2 2011-12-06 12:34:36.458354 0:00:07.954921
    Process-3 2011-12-06 12:34:36.546656 0:00:08.048761
    

    PS. As zeekay pointed out in the comments, The GIL battle is only severe for CPU-bound tasks. It should not be a problem for IO-bound tasks.

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

Sidebar

Related Questions

I've recently started developing my first web app with GAE and Python, and it
Am developing an app using zend framework. I want to update some columns in
Right now i am developing an application using Google App Engine (GAE). GAE doesnt
i'm developing an app that make requests to the Musicbrainz webservice. I read in
I'm developing web app that user can save his/her work to server. The data
I am developing an app for GAE. Having installed the feedparser module with setuptools,
I am developing app with finding audio frequency by using FFT. In my app,
I developing an app that is read-only. The data has relationships, so I cannot
I'm developing an app where I use performSelectorInBackground to do some sync task in
When researching Google App Engine (GAE), it's clear that using Django is wildly popular

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.