I'm using Python 3.4.3. While my event loop is working, I'm communicating with some infinite process (tail -f in the example). Once all other tasks are done, I cancel tail task as well, wait for it to finish and close the loop.
import asyncio
import time
@asyncio.coroutine
def tailf():
try:
create = asyncio.create_subprocess_exec(
'tail', '-f', '/tmp/file',
stdout=asyncio.subprocess.PIPE,
)
proc = yield from create
while True:
l = yield from proc.stdout.readline()
print(l)
except BaseException as e:
print('CAUGHT', type(e))
@asyncio.coroutine
def task():
yield from asyncio.sleep(1)
loop = asyncio.get_event_loop()
tailf_task = loop.create_task(tailf())
loop.run_until_complete(asyncio.wait([task()]))
tailf_task.cancel()
loop.run_until_complete(asyncio.wait([tailf_task]))
loop.close()
The output is:
b'123\n'
CAUGHT <class 'concurrent.futures._base.CancelledError'>
Exception ignored in: Exception ignored in:
Do echo 123 > /tmp/file to get the same result before running the example.
It all works as expected except I got that warning at the end of the script.
I believe the reason is yield from proc.stdout.readline() being interrupted, but I would like to catch the exception in that case.
So, the question is: what am I doing wrong?
And the more broad one: how do I debug such warning next time?
except BaseException as e:, does that catch it?GeneratorExit, which is not meant to be caught withexcept Exception, onlyBaseException. But I guess that's not it.