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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:33:17+00:00 2026-05-23T17:33:17+00:00

I have a process, that needs to perform a bunch of actions later (after

  • 0

I have a process, that needs to perform a bunch of actions “later” (after 10-60 seconds usually). The problem is that those “later” actions can be a lot (1000s), so using a Thread per task is not viable. I know for the existence of tools like gevent and eventlet, but one of the problem is that the process uses zeromq for communication so I would need some integration (eventlet already has it).

What I’m wondering is What are my options? So, suggestions are welcome, in the lines of libraries (if you’ve used any of the mentioned please share your experiences), techniques (Python’s “coroutine” support, use one thread that sleeps for a while and checks a queue), how to make use of zeromq’s poll or eventloop to do the job, or something else.

  • 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-23T17:33:17+00:00Added an answer on May 23, 2026 at 5:33 pm

    consider using a priority queue with one or more worker threads to service the tasks. The main thread can add work to the queue, with a timestamp of the soonest it should be serviced. Worker threads pop work off the queue, sleep until the time of priority value is reached, do the work, and then pop another item off the queue.

    How about a more fleshed out answer. mklauber makes a good point. If there’s a chance all of your workers might be sleeping when you have new, more urgent work, then a queue.PriorityQueue isn’t really the solution, although a “priority queue” is still the technique to use, which is available from the heapq module. Instead, we’ll make use of a different synchronization primitive; a condition variable, which in python is spelled threading.Condition.

    The approach is fairly simple, peek on the heap, and if the work is current, pop it off and do that work. If there was work, but it’s scheduled into the future, just wait on the condition until then, or if there’s no work at all, sleep forever.

    The producer does it’s fair share of the work; every time it adds new work, it notifies the condition, so if there are sleeping workers, they’ll wake up and recheck the queue for newer work.

    import heapq, time, threading
    
    START_TIME = time.time()
    SERIALIZE_STDOUT = threading.Lock()
    def consumer(message):
        """the actual work function.  nevermind the locks here, this just keeps
           the output nicely formatted.  a real work function probably won't need
           it, or might need quite different synchronization"""
        SERIALIZE_STDOUT.acquire()
        print time.time() - START_TIME, message
        SERIALIZE_STDOUT.release()
    
    def produce(work_queue, condition, timeout, message):
        """called to put a single item onto the work queue."""
        prio = time.time() + float(timeout)
        condition.acquire()
        heapq.heappush(work_queue, (prio, message))
        condition.notify()
        condition.release()
    
    def worker(work_queue, condition):
        condition.acquire()
        stopped = False
        while not stopped:
            now = time.time()
            if work_queue:
                prio, data = work_queue[0]
                if data == 'stop':
                    stopped = True
                    continue
                if prio < now:
                    heapq.heappop(work_queue)
                    condition.release()
                    # do some work!
                    consumer(data)
                    condition.acquire()
                else:
                    condition.wait(prio - now)
            else:
                # the queue is empty, wait until notified
                condition.wait()
        condition.release()
    
    if __name__ == '__main__':
        # first set up the work queue and worker pool
        work_queue = []
        cond = threading.Condition()
        pool = [threading.Thread(target=worker, args=(work_queue, cond))
                for _ignored in range(4)]
        map(threading.Thread.start, pool)
    
        # now add some work
        produce(work_queue, cond, 10, 'Grumpy')
        produce(work_queue, cond, 10, 'Sneezy')
        produce(work_queue, cond, 5, 'Happy')
        produce(work_queue, cond, 10, 'Dopey')
        produce(work_queue, cond, 15, 'Bashful')
        time.sleep(5)
        produce(work_queue, cond, 5, 'Sleepy')
        produce(work_queue, cond, 10, 'Doc')
    
        # and just to make the example a bit more friendly, tell the threads to stop after all
        # the work is done
        produce(work_queue, cond, float('inf'), 'stop')
        map(threading.Thread.join, pool)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have process that needs to create a bunch of records in the database
I have a C++ process which has a thread that needs to send floats
I have a multi-process python application (processes are spawned by uwsgi) that needs to
I have a timer that needs to not process its elapsed event handler at
I have written a Scala (2.9.1-1) application that needs to process several million rows
I have a WPF app that from time to time needs to perform a
I have a process that spawns a helper process. Sometimes I need to debug
I have a process that pickles a dictionary using Python 3.2. I then need
I need to have a background process that runs independent of my app and
I have a list of strings that I need to pass to a process

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.