Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I Spawn threads from two different objects and coordinate them in python v2.7?
    primarykey
    data
    text
    <p>I am trying to combine the answers I got from two different python questions.</p> <p>Here is the the first question and answer. Basically I just wanted to spawn two threads, one to powerDown() and the other to powerUp(), where powerUp() pends on powerDown()</p> <p><a href="https://stackoverflow.com/questions/18989472/how-to-spawn-a-thread-inside-another-thread-in-the-same-object-in-python">How to spawn a thread inside another thread in the same object in python?</a></p> <pre><code>import threading class Server(threading.Thread): # some code def run(self): self.reboot() # This is the top level function called by other objects def reboot(self): # perhaps add a lock if not hasattr(self, "_down"): self._down = threading.Thread(target=self.__powerDown) self._down.start() up = threading.Thread(target=self.__powerUp) up.start() def __powerDown(self): # do something def __powerUp(self): if not hasattr(self, "_down"): return self._down.join() # do something del self._down </code></pre> <hr> <p>Here is the the second question and answer. Basically I wanted to start a thread, and then call a function of the object.</p> <p><a href="https://stackoverflow.com/questions/19033818/how-call-a-function-on-a-running-thread-python">How to call a function on a running Python thread</a></p> <pre><code>import queue import threading class SomeClass(threading.Thread): def __init__(self, q, loop_time = 1.0/60): self.q = q self.timeout = loop_time super(SomeClass, self).__init__() def onThread(self, function, *args, **kwargs): self.q.put((function, args, kwargs)) def run(self): while True: try: function, args, kwargs = self.q.get(timeout=self.timeout) function(*args, **kwargs) except queue.Empty: self.idle() def idle(self): # put the code you would have put in the `run` loop here def doSomething(self): pass def doSomethingElse(self): pass </code></pre> <p>Here is combined idea code. Basically I wanted to spawn a thread, then then queue up a functions to execute, which in this case is reboot(). reboot() in turns creates two threads, the powerDown() and powerUp() threads, where powerDown() pends on powerUp()</p> <pre><code>import threading import Queue class Server(threading.Thread): def __init__(self, q, loop_time = 1.0/60): self.q = q self.timeout = loop_time super(Server, self).__init__() def run(self): while True: try: function, args, kwargs = self.q.get(timeout=self.timeout) function(*args, **kwargs) except queue.Empty: self.idle() def idle(self): # put the code you would have put in the `run` loop here # This is the top level function called by other objects def reboot(self): self.__onthread(self.__reboot) def __reboot(self): if not hasattr(self, "_down"): self._down = threading.Thread(target=self.__powerDown) self._down.start() up = threading.Thread(target=self.__powerUp) up.start() def __onThread(self, function, *args, **kwargs): self.q.put((function, args, kwargs)) def __powerDown(self): # do something def __powerUp(self): if not hasattr(self, "_down"): return self._down.join() # do something del self._down </code></pre> <p>All work, except when I create two Server subclasses.</p> <pre><code>class ServerA(Server): pass class ServerB(Server): pass </code></pre> <p>Here is the code that instatiats both subclasses, and call the start() and reboot functions</p> <pre><code>serverA = ServerA(None) serverB = ServerB(None) serverA.start() serverB.start() serverA.reboot() serverB.reboot() </code></pre> <p>I expect serverA.reboot() and serverB.reboot() to happen concurrently, which is what I want, but they DO NOT! serverB.reboot() gets executed after serverA.reboot() is done. That is, if I put print statements, I get</p> <pre><code>serverA started serverB started serverA.reboot() called serverA.__powerDown called serverA.__powerUp called serverB.reboot() called serverB.__powerDown called serverB.__powerUp called </code></pre> <p>I know for a fact that it takes longer for ServerA to reboot, so I expect something like this</p> <pre><code>serverA started serverB started serverA.reboot() called serverB.reboot() called serverA.__powerDown called serverB.__powerDown called serverB.__powerUp called serverA.__powerUp called </code></pre> <p>I hope that makes sense. If it does, why aren't my reboot() functions happening simultaneously?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload