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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:23:24+00:00 2026-05-14T06:23:24+00:00

Let’s say I have an object who’s class definition looks like: class Command: foo

  • 0

Let’s say I have an object who’s class definition looks like:

class Command:
  foo = 5
  def run(self, bar):
    time.sleep(1)
    self.foo = bar
    return self.foo

If this class is instantiated once, but different threads are hitting its run method (via an HTTP request, handled separately) passing in different args, what is the best method to queue them? Can this be done in the class definition itself?

I am using an XML RPC server (separate class). For simplicity’s sake, we can say it has one instance of the Command class instantiated as a class variable. When Command.run() is being hit by two separate threads, how can I make sure that one run() method is complete before the next one is started?

I could do something like:

  while self.busy:
    time.sleep(1)
    self.busy = true
    ...
    self.busy = false
    return self.foo

but that, as far as I know, would not give priority to the oldest request.

I realize how redundant this whole exercise sounds, since I could just run the XML-RPC server synchronously. But, to make a long story short, there are multiple Command instantiations and I do not want to block requests for one because another is busy.

I hope this makes more sense.

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-14T06:23:25+00:00Added an answer on May 14, 2026 at 6:23 am

    Here’s a relatively simple approach (ignores exceptions, attribute-access, special methods, etc):

    import Queue
    import threading
    
    def serialize(q):
      """runs a serializer on queue q: put [-1]*4 on q to terminate."""
      while True:
        # get output-queue for result, a callable, its args and kwds
        out_q, tocall, args, kwds = q.get()
        if out_q == -1:
          return
        result = tocall(*args, **kwds)
        out_q.put(result)
    
    class WrapCall(object):
      """Wraps a callable to serialize calls to it."""
    
      def __init__(self, inq, ouq, tocall):
        self.inq = inq
        self.ouq = ouq
        self.tocall = tocall
    
      def __call__(self, *a, **k):
        self.inq.put((self.ouq, self.tocall, a, k))
        if self.ouq is None:
          return None
        return self.ouq.get()
    
    class WrapObj(object):
      """Wraps any object to serialize all calls to its methods."""
    
      def __init__(self, obj):
        self._o = obj
        self._q = Queue.Queue()
        t = threading.Thread(target=serialize, args=(self._q,))
        t.setDaemon(True)
        t.start()
        self.t = t
    
      def __getattr__(self, n):
        """Wraps methods of self.w into an appropriate WrapCall instance."""
        towrap = getattr(self._o, n)
        if not callable(towrap):
          raise TypeError('Cannot wrap noncallable attribute %r (type: %s)'
                           % (n, type(towrap)))
        q = Queue.Queue()
        return WrapCall(self._q, q, towrap)
    
      def WrapperWait(self):
        """Return only when self.t has served all pending requests."""
        q = Queue.Queue()
        w = WrapCall(self.__q, q, lambda: None)
        return w()
    

    With this “serializer”, you can do

    myobj = WrapObj(Command())
    

    and now all calls to myobj’s (non-special) methods are serialized in a thread-safe way.

    For your specific case, where there’s only one method on the object, you could simplify this a bit further, but this is an already-simplified version of something I wrote and use often (supporting attribute getting and setting, special methods, etc, is a tad too complex to be worth it; the complete version does support catching and reraising exceptions raised in the wrapper object’s methods, an optimization for calls whose results or exceptions you don’t care about, and a few more tweaks, but not serialization of attributes and special methods).

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

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Windows does not want to waste electricity by updating the… May 15, 2026 at 2:02 pm
  • Editorial Team
    Editorial Team added an answer Not one liner but you can put on one line… May 15, 2026 at 2:02 pm
  • Editorial Team
    Editorial Team added an answer To check for existence of process with a given id,… May 15, 2026 at 2:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.