classification
Title: Run Tasks cannot Concurrent
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, youngchaos, yselivanov
Priority: normal Keywords:

Created on 2018-12-13 08:54 by youngchaos, last changed 2018-12-13 13:30 by asvetlov. This issue is now closed.

Messages (3)
msg331749 - (view) Author: (youngchaos) Date: 2018-12-13 08:58
import struct, asyncio, os

async def readdev(dev):
    while True:
        buf=os.read(dev, 480)
        print([struct.unpack('llHHi', buf[i*24:(i+1)*24]) for i in range(len(buf)//24)])

mouse=os.open('/dev/input/event2', os.O_RDONLY)
kbd=os.open('/dev/input/event5', os.O_RDONLY)

async def main(mouse,kbd):
    await asyncio.gather(readdev(mouse),readdev(kbd))

asyncio.run(main(mouse,kbd))
msg331750 - (view) Author: (youngchaos) Date: 2018-12-13 09:09
#!/usr/bin/env python
# coding: utf-8

import struct, asyncio, os

async def readdev(dev):
    while True:
        buf=os.read(dev, 480)
        print([struct.unpack('llHHi', buf[i*24:(i+1)*24]) for i in range(len(buf)//24)])

mouse=os.open('/dev/input/event2', os.O_RDONLY)
kbd=os.open('/dev/input/event5', os.O_RDONLY)

async def main(mouse,kbd):
    await asyncio.gather(readdev(mouse),readdev(kbd))

asyncio.run(main(mouse,kbd))
------------
python 3.7.1
Linux 4.19.6-1-MANJARO #1 SMP PREEMPT Sat Dec 1 12:21:26 UTC 2018 x86_64 GNU/Linux

run code, output:
[(1544681359, 149693, 2, 0, -1), (1544681359, 149693, 2, 1, 1), (1544681359, 149693, 0, 0, 0)]
sdffvsdgsdgsdgsdgsdgs

mouse move output line1 format, and keyboard press output line2 format

await asyncio.gather(readdev(kbd)) and key press can output lists
howto both output lists? It is a bug?
msg331760 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-12-13 13:30
A task should have `await` inside to give to loop a chance to switch to another task (or get canceled).

It is not an asyncio bug but a part of specified behavior.

Your code could be modified as

async def readdev(dev):
    while True:
        buf=os.read(dev, 480)
        print([struct.unpack('llHHi', buf[i*24:(i+1)*24]) for i in range(len(buf)//24)])
        await asyncio.sleep(0)

to use tasks switching
History
Date User Action Args
2018-12-13 13:30:45asvetlovsetstatus: open -> closed
resolution: not a bug
messages: + msg331760

stage: resolved
2018-12-13 09:09:16youngchaossetmessages: + msg331750
2018-12-13 08:58:21youngchaossetmessages: + msg331749
2018-12-13 08:54:45youngchaoscreate