title: Python process wait
body: <p>I have this application that shares work across multiple devices and starts processes on them, they often need to synchronize to each other.</p>
  <ol>
  <li>A web app opens an SSH connection to servers.</li>
  <li>It starts <em>n</em> Python instances per server.</li>
  <li>Then closes the SSH connection.</li>
  </ol>
  <p>After that the only way each agent will communicate is with MQTT (Publish / subscribe on topics). The MQTT library I'm using is Paho-MQTT.</p>
  <p>My current issue is that I can't find a good way to make them wait. I feel like <code>while true : sleep</code> is not a good way of doing it but I have no clue on how to do it better.</p>
  <p>My current code looks like this:</p>
  <pre class="lang-py prettyprint-override"><code>def wake_up(self, client, userdata, message) -&gt; None:
     self.wake_up_token += 1
  
  def wait(self) -&gt; None:
     while self.wake_up_token == 0:
         sleep(self.wait_delay)
     self.wake_up_token -= 1
  </code></pre>
  <p>So far my only idea would be to find the best wait delay by dichotomy tho it will be time consuming so I'm hoping on a better solution.</p>
  <p>Note:</p>
  <pre class="lang-py prettyprint-override"><code>def wait():
     while self.wake_up_token == 0:
        pass
  </code></pre>
  <p>is a terrible solution because the process will be constantly in competition to check the condition and it makes the system thousands of times slower.</p>
  <p>Minimal example I guess: (1 agent, 1 scheduler)</p>
  <pre class="lang-py prettyprint-override"><code>from paho.mqtt.client import Client
  from time import sleep
  
  class Agent:
  
      def __init__(self, broker_ip: str, client_id: str):
          self.client: Client = Client(client_id=client_id)
          self.client.username_pw_set(username=&quot;something&quot;, password=&quot;veryverysecured&quot;)
          self.client.connect(host=broker_ip)
          self.client.subscribe(&quot;wake_up_topic&quot;)
          self.client.message_callback_add(&quot;wake_up_topic&quot;, self.wake_up)
          self.client.loop_start()
  
          self.wake_up_token = 0
          
      def wake_up(self, client, userdata, message) -&gt; None:
          self.wake_up_token += 1
      
      def wait(self) -&gt; None:
          while self.wake_up_token == 0:
              sleep(self.wait_delay)
          self.wake_up_token -= 1
      
      def run(self):
          while True:
              # do something
              self.client.publish(topic=&quot;agent/action_done&quot;, message=&quot;&quot;)
              self.wait()
              # do something
  
  class Scheduler:
  
      def __init__(self, broker_ip: str):
          self.client: Client = Client(client_id=&quot;scheduler&quot;)
          self.client.username_pw_set(username=&quot;something&quot;, password=&quot;veryverysecured&quot;)
          self.client.connect(host=broker_ip)
          self.client.subscribe(&quot;agent/action_done&quot;)
          self.client.message_callback_add(&quot;agent/action_done&quot;, self.wake_up)
          self.client.loop_start()
  
          self.wake_up_token = 0
  
      def wake_up(self, client, userdata, message) -&gt; None:
          self.wake_up_token += 1
      
      def wait(self) -&gt; None:
          while self.wake_up_token == 0:
              sleep(self.wait_delay)
          self.wake_up_token -= 1
      
      def run(self):
          while True:
  
              #agents are doing their thing
  
              self.wait()
              self.client.publish(topic=&quot;wake_up_topic&quot;, message=&quot;&quot;)
  </code></pre>
  <p>I don't know if it is useful, but the goal of the whole project is to build a <a href="https://en.wikipedia.org/wiki/Multi-agent_system" rel="nofollow noreferrer">MAS</a> frameworks that shares the agents across multiple servers.</p>