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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:47:54+00:00 2026-05-29T16:47:54+00:00

A few days ago I has asked a question on SO about helping me

  • 0

A few days ago I has asked a question on SO about helping me design a paradigm for structuring multiple HTTP requests

Here’s the scenario. I would like a have a multi-producer, multi-consumer system. My producers crawl and scrape a few sites and add the links that it finds into a queue. Since I’ll be crawling multiple sites, I would like to have multiple producers/crawlers.

The consumers/workers feed off this queue, make TCP/UDP requests to these links and saves the results to my Django DB. I would also like to have multiple-workers as each queue item is totally independent of each other.

People suggested that use a coroutine library for this i.e. Gevent or Eventlet. Having never worked with coroutines, I read that even though the programming paradigm is similar to threaded paradigms, only one thread is actively executing but when blocking calls occur – such as I/O calls – the stacks are switched in-memory and the other green thread takes over until it encounters some sort of a blocking I/O call. Hopefully I got this right? Here’s the code from one of my SO posts:

import gevent
from gevent.queue import *
import time
import random

q = JoinableQueue()
workers = []
producers = []


def do_work(wid, value):
    gevent.sleep(random.randint(0,2))
    print 'Task', value, 'done', wid


def worker(wid):
    while True:
        item = q.get()
        try:
            print "Got item %s" % item
            do_work(wid, item)
        finally:
            print "No more items"
            q.task_done()


def producer():
    while True:
        item = random.randint(1, 11)
        if item == 10:
            print "Signal Received"
            return
        else:
            print "Added item %s" % item
            q.put(item)


for i in range(4):
    workers.append(gevent.spawn(worker, random.randint(1, 100000)))

# This doesn't work.
for j in range(2):
    producers.append(gevent.spawn(producer))

# Uncommenting this makes this script work.
# producer()

q.join()

This works well because the sleep calls are blocking calls and when a sleep event occurs, another green thread takes over. This is a lot faster than sequential execution.
As you can see, I don’t have any code in my program that purposely yields the execution of one thread to another thread. I fail to see how this fits into scenario above as I would like to have all the threads executing simultaneously.

All works fine, but I feel the throughput that I’ve achieved using Gevent/Eventlets is higher than the original sequentially running program but drastically lower than what could be achieved using real-threading.

If I were to re-implement my program using threading mechanisms, each of my producers and consumers could simultaneously be working without the need to swap stacks in and out like coroutines.

Should this be re-implemented using threading? Is my design wrong? I’ve failed to see the real benefits of using coroutines.

Maybe my concepts are little muddy but this is what I’ve assimilated. Any help or clarification of my paradigm and concepts would be great.

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-29T16:47:58+00:00Added an answer on May 29, 2026 at 4:47 pm

    As you can see, I don’t have any code in my program that purposely
    yields the execution of one thread to another thread. I fail to see
    how this fits into scenario above as I would like to have all the
    threads executing simultaneously.

    There is a single OS thread but several greenlets. In your case gevent.sleep() allows workers to execute concurrently. Blocking IO calls such as urllib2.urlopen(url).read() do the same if you use urllib2 patched to work with gevent (by calling gevent.monkey.patch_*()).

    See also A Curious Course on Coroutines and Concurrency to understand how a code can work concurrently in a single threaded environment.

    To compare throughput differences between gevent, threading, multiprocessing you could write the code that compatible with all aproaches:

    #!/usr/bin/env python
    concurrency_impl = 'gevent' # single process, single thread
    ##concurrency_impl = 'threading' # single process, multiple threads
    ##concurrency_impl = 'multiprocessing' # multiple processes
    
    if concurrency_impl == 'gevent':
        import gevent.monkey; gevent.monkey.patch_all()
    
    import logging
    import time
    import random
    from itertools import count, islice
    
    info = logging.info
    
    if concurrency_impl in ['gevent', 'threading']:
        from Queue import Queue as JoinableQueue
        from threading import Thread
    if concurrency_impl == 'multiprocessing':
        from multiprocessing import Process as Thread, JoinableQueue
    

    The rest of the script is the same for all concurrency implementations:

    def do_work(wid, value):
        time.sleep(random.randint(0,2))
        info("%d Task %s done" % (wid, value))
    
    def worker(wid, q):
        while True:
            item = q.get()
            try:
                info("%d Got item %s" % (wid, item))
                do_work(wid, item)
            finally:
                q.task_done()
                info("%d Done item %s" % (wid, item))
    
    def producer(pid, q):
        for item in iter(lambda: random.randint(1, 11), 10):
            time.sleep(.1) # simulate a green blocking call that yields control
            info("%d Added item %s" % (pid, item))
            q.put(item)
        info("%d Signal Received" % (pid,))
    

    Don’t execute code at a module level put it in main():

    def main():
        logging.basicConfig(level=logging.INFO,
                            format="%(asctime)s %(process)d %(message)s")
    
        q = JoinableQueue()
        it = count(1)
        producers = [Thread(target=producer, args=(i, q)) for i in islice(it, 2)]
        workers = [Thread(target=worker, args=(i, q)) for i in islice(it, 4)]
        for t in producers+workers:
            t.daemon = True
            t.start()
    
        for t in producers: t.join() # put items in the queue
        q.join() # wait while it is empty
        # exit main thread (daemon workers die at this point)
    
    if __name__=="__main__":    
       main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few days ago I asked this question about jquery ajax function invoking action
so I asked here few days ago about C# and its principles. Now, if
few days ago I asked here about implementing USB. Now, If I may, would
Few days ago I have asked a question about 1,2 and 3. degree connections.
few days ago i asked about how to get all running processes in the
few days ago i read tutorial about GenericRepository and Unit Of Work patterns http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
Guys I've asked few days ago a question and didn't have really time to
I originally asked this question on the Highcharts forum a few days ago but
A few days ago I asked the following question: How to draw graphics as
I asked something similar a few days ago, here is my issue. My professor

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.