asyncio queue consumer coroutine : <p>I have a <code>asyncio.Protocol</code> subclass receiving data from a server.
I am storing this data (each line, because the data is text) in a <code>asyncio.Queue</code>.</p>

<pre><code>import asyncio

q = asyncio.Queue()

class StreamProtocol(asyncio.Protocol):
    def __init__(self, loop):
        self.loop = loop
        self.transport = None

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        for message in data.decode().splitlines():
            yield q.put(message.rstrip())

    def connection_lost(self, exc):
        self.loop.stop()

loop = asyncio.get_event_loop()
coro = loop.create_connection(lambda: StreamProtocol(loop),
                              '127.0.0.1', '42')
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
</code></pre>

<p>I want to have another coroutine responsible for consuming the data in the queue and processing it.</p>

<ul>
<li>Should this be a <code>asyncio.Task</code>?</li>
<li>What if the queue becomes empty because for a few seconds no data is received? How can I make sure my consumer doesn't stop (<code>run_until_complete</code>)?</li>
<li>Is there a cleaner way than using a global variable for my queue?</li>
</ul>
