1

I’m working on a server/client program that , upon client signal, the server sends the client a series of messages, the series of messages through the function:

def messages():
    messages = [“first”, “second”, “third”, “fourth”]
    yield element
    time.sleep(10)

I’m trying to learn how to use python3.6 async to listen to for these messages, and check each second to see if another message has been received.

async def get_message(response): 
    async for element in response: 
        print (“got a message!”)
        print (element)
    else: 
        print (“no message received”)

However, when I run this, I get a runtime warning of coroutine ‘get_message” was never awaited. I don’t understand how “await” fits in here.

This question uses asyncio, but I don’t understand where I would be calling await asyncio(1) in my code? I’m only trying to modify the client side code, and it looks like in PEP525, they put the await asyncio.sleep(delay) in what would be my server side code.

Edit:

Assuming that the server's messages are stored in response, I currently just call:

def run(): 
    get_message(response)
Brendan
  • 11
  • 3
  • maybe `await` doesn't fit in your code ? maybe you should use it ? – furas Nov 29 '17 at 01:43
  • I'm confused as to where I should use it - I put await asyncio.sleep(1) inside the function, but none of the error messages are printing. Sorry, I am quite new to this, so I might be missing something fundamental :( – Brendan Nov 29 '17 at 05:42
  • coroutines are best used with IO, and need to be driven by an event loop. You don't use them like "regular" functions. – Keith Jan 21 '18 at 01:19

0 Answers0