title: How do I get my asyncio client to call a socket server and waiting for response
body: <p>I am working with an <code>asyncio.Protocol</code> server where the purpose is for the client to call the server, but <strong>wait</strong> until the server has responded and data is returned before stopping the client loop.</p>
  
  <p>Based on the asyncio doc Echo Client and Server here: <a href="https://docs.python.org/3/library/asyncio-protocol.html#protocol-example-tcp-echo-server-and-client" rel="nofollow">https://docs.python.org/3/library/asyncio-protocol.html#protocol-example-tcp-echo-server-and-client</a> , results of transport.write(...) are returned immediately when called.</p>
  
  <p>Through experience, calling <code>loop.run_until_complete(coroutine)</code> fails with <code>RuntimeError: Event loop is running.</code></p>
  
  <p>Running <code>asyncio.sleep(n)</code> in the <code>data_received()</code> method of the server doesn't have any effect either. </p>
  
  <p><code>yield from asyncio.sleep(n)</code> and <code>yield from asyncio.async(asyncio.sleep(n))</code> in <code>data_received()</code> both hang the server.</p>
  
  <p>My question is, how do I get my client to wait for the server to write a response before giving back control?</p>