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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:06:54+00:00 2026-06-11T22:06:54+00:00

Possible Duplicate: Asynchronous HTTP calls in Python I have a Django view which needs

  • 0

Possible Duplicate:
Asynchronous HTTP calls in Python

I have a Django view which needs to retrieve search results from multiple web services, blend the results together, and render them. I’ve never done any multithreading in Django before. What is a modern, efficient, safe way of doing this?

I don’t know anything about it yet, but gevent seems like a reasonable option. Should I use that? Does it play well with Django? Should I look elsewhere?

  • 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-11T22:06:56+00:00Added an answer on June 11, 2026 at 10:06 pm

    Not sure about gevent. The simplest way is to use threads[*]. Here’s a simple example of how to use threads in Python:

    # std lib modules. "Batteries included" FTW.
    import threading
    import time
    
    thread_result = -1
    
    def ThreadWork():
      global thread_result
      thread_result = 1 + 1
      time.sleep(5)  # phew, I'm tiered after all that addition!
    
    my_thread = threading.Thread(target=ThreadWork)
    my_thread.start()  # This will call ThreadWork in the background.
                       # In the mean time, you can do other stuff
    y = 2 * 5  # Completely independent calculation.
    my_thread.join()  # Wait for the thread to finish doing it's thing.
                      # This should take about 5 seconds,
                      # due to time.sleep being called
    print "thread_result * y =", thread_result * y
    

    You can start multiple threads, have each make different web service calls, and join on all of those threads. Once all those join calls have returned, the results are in, and you’ll be able to blend them.

    more advanced tips: You should call join with a timeout; otherwise, your users might be waiting indefinitely for your app to send them a response. Even better would be for you to make those web service calls before the request arrives at your app; otherwise, the responsiveness of your app is at the mercy of the services that you rely on.

    caveat about threading in general: Be careful with data that can be accessed by two (or more) different threads. Access to the same data needs to be “synchronized”. The most popular synchronization device is a lock, but there is a plethora of others. threading.Lock implements a lock. If you’re not careful about synchronization, you’re likely to write a “race condition” into your app. Such bugs are notoriously difficult to debug, because they cannot be reliably reproduced.

    In my simple example, thread_result was shared between my_thread and the main thread. I didn’t need any locks, because the main thread did not access thread_result until my_thread terminated. If I hadn’t called my_thread.join, the result would some times be -10 instead of 20. Go ahead and try it yourself.

    [*] Python doesn’t have true threading in the sense that concurrent threads do not execute simulatneously, even if you have idle cores. However, you still get concurrent execution; when one thread is blocked, other threads can execute.

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

Sidebar

Related Questions

Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Returning a value from thread? I have this code: //Asynchronously start the
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How can I upload files asynchronously with JQuery? I have a file
Possible Duplicate: How can I get jQuery to perform a synchronous, rather than asynchronous,
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like

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.