title: Passing data between functions from endless while loop
body: <p>Been struggling to get this to work because I can't use <code>return</code> in a while loop that is not going to end.</p>
  <p>In a nutshell, I'm receiving a message in a socket client in one function <code>receive()</code> (the endless loop) and need to pass the result of that incoming message to <code>main()</code>. Trying to use <code>yield</code>, since I'm not sure what else would achieve this. I created another function to try and capture the <code>yield</code> in the <code>receive()</code> function.</p>
  <p>I know the initial message reaches the server because it outputs the message, and I know the client is receiving the server's confirmation message because it's printing it. I just am having no luck passing that data to <code>main()</code>, so the rest of the code won't work right.</p>
  <p>I'm very new to this, so I'm probably doing this horribly wrong. I should be using classes to share data more easily, but don't grasp them enough yet. Hopefully using yield or something else can get around that.</p>
  <h3>receive function:</h3>
  <pre class="lang-py prettyprint-override"><code>def receive():
      while True:
          try:
              incoming = client.recv(2048).decode(FORMAT)
              if 'RECEIVED' in incoming:
                  confirmation = 'confirmed'
                  yield confirmation
              print(incoming)
          except:
              print(&quot;Connection interrupted.&quot;)
              client.close()
              break
  
  #------------
  # also tried
  #------------
  def receive():
      while True:
          try:
              incoming = client.recv(2048).decode(FORMAT)
              if 'RECEIVED:COMPLETE' in incoming:
                  confirmation = 'confirmed'
              else:
                  confirmation = 'unconfirmed'
              yield confirmation
          except:
              print(&quot;Connection interrupted.&quot;)
              client.close()
              break
  
  </code></pre>
  <h3>return function:</h3>
  <pre class="lang-py prettyprint-override"><code>def pass_return(passed_return_value):
      passed_return_value
  </code></pre>
  <h3>main function (with various tests)</h3>
  <pre class="lang-py prettyprint-override"><code>def main():
      pass_return(receive())
      # Bunch of code
          if something == True:
              send(&quot;some message&quot;)
              time.sleep(.25)
              try:
                  if confirmation == 'confirmed':
                      # do the business here
  
  #------------
  # also tried
  #------------
  def main():
      # Bunch of code
          if something == True:
              send(&quot;some message&quot;)
              time.sleep(.25)
              pass_return(receive())
              try:
                  if confirmation == 'confirmed':
                      # do the business here
  
  #------------
  # also tried
  #------------
  def main():
      r = pass_return(receive())
      # Bunch of code
          if something == True:
              send(&quot;some message&quot;)
              time.sleep(.25)
              try:
                  if r == 'confirmed':
                      # do the business here
  
  #------------
  # even tried
  #------------
  def main():
      # Bunch of code
          if something == True:
              send(&quot;some message&quot;)
              time.sleep(.25)
              r = pass_return(receive())
              try:
                  if r == 'confirmed':
                      # do the business here
  </code></pre>
  <p>I'm declaring the variable <code>confirmation</code> OUTSIDE of <code>main()</code> and <code>receive()</code> (atop the file where my constants are) or else I get an error of <code>confirmation is undefined</code>.</p>
  <p>If I <code>print</code> <code>confirmation</code> in <code>main()</code>, it either prints nothing or <code>None</code>, so my guess is that it's just getting the initial empty value of <code>confirmation</code> and not the <code>yield</code>.</p>
  <pre class="lang-py prettyprint-override"><code># constants above here
  confirmation = str()
  
  # code and such
  def pass_return(passed_return_value):
      passed_return_value
  def receive():
      #code...
  def main():
      #code...
  
  if __name__ == '__main__':
      main()
  </code></pre>