Problems with asyncio in 3.4.2 - it just ends for some reason

New to python and spending many hours reading documents and other code, I can’t imagine a new module asyncioin Python 3. It continues to end without a stack trace to give me a key and should work forever, but it doesn’t.

The basic concept of the process that I am trying to emulate is as follows:

read from port: open port → read data (variable length) → place in queue1

then process the data: get data from the condition queue1 → -> result put on queue2

then write to the port: get data from queue2 and write to the port

loop top to top

Note. The data on the port is sporadic, variable length and several blocks may go out of the “sequence”, so I use asyncio. I understand that it asynciowill allow me to get a case when a block arrives, and then before my application - for example, a call get_io_from_port()facilitates several executions of a joint procedure. This is why I use queues to provide non-blockingprocess_queue()

My sample toy code:

import queue
import asyncio

@asyncio.coroutine
def process_queue(q1, q2):
    tmp = q1.Get()
    if tmp == 'ABCDEF':
        q2.put('12345')
    elif tmp == 'GHIJKL':
        q2.put =('67890')
    else:
        print('There is a data error')

@asyncio.coroutine
def put_io_to_port(writer, q2):

    if not q2.empty():
        try:
                writer.write(q2.get())

        except IOError as e:
            print('OUT Port issue: ', e)

@asyncio.coroutine
def get_io_from_port(reader, q1):

    try:
        data_i = yield from reader.read(1200)
        q1.put(data_i)

    except IOError as e:
        print('IN Port issue: ', e)

def main():

    q1 = queue()
    q2 = queue()

    loop = asyncio.get_event_loop()     # main loop declaration
    reader, writer = yield from asyncio.open_connection('192.168.1.103', 5555)
                # high-level call open streams - read and write

    print('Start')
    tasks = [
        asyncio.async(get_io_from_port(reader,q1)),
        asyncio.async(process_queue(q1, q2)),
        asyncio.async(put_io_to_port(writer, q2)),]  # do these tasks - in this order

    loop.run_forever(tasks)     # loop through on main loop forever
    loop.close()

if __name__ == '__main__':
    main()

Also, aside - how to debug this code - i.e. track? What methods can be offered? I use Eclipse and PyDev, but to no avail.

+4
source share
2 answers

. -, main , yield from, . ,

if __name__ == "__main__":
    main()

main ; main() -, ( ). - main . main loop.run_until_complete.

queue, . queue.get(), , , asyncio , , . asyncio.Queue.

put_io_to_port. q2, , , put_io_to_port , process_queue queue. , , if not q2.empty() put_io_to_port .

, , asyncio.async, . , # do these tasks, in this order, , asyncio.async. , . , , :

yield from get_io_from_port(reader,q1)
yield from process_queue(q1, q2)
yield from put_io_to_port(writer, q2)

. ; , , , , .

(q1.Get(), q2.put =(...) ..).

, , :

import queue
import asyncio

@asyncio.coroutine
def process_queue(q1, q2):
    while True:
        tmp = yield from q1.get()
        if tmp == 'ABCDEF':
            yield from q2.put('12345')
        elif tmp == 'GHIJKL':
            yield from q2.put('67890')
        else:
            print('There is a data error')

@asyncio.coroutine
def put_io_to_port(writer, q2):
    while True:
        try:
            data = yield from q2.get()
            writer.write(data)
        except IOError as e:
            print('OUT Port issue: ', e)

@asyncio.coroutine
def get_io_from_port(reader, q1):
    while True:
        try:
            data_i = yield from reader.read(1200)
            yield from q1.put(data_i)
        except IOError as e:
            print('IN Port issue: ', e)

@asyncio.coroutine
def main():
    q1 = asyncio.Queue()
    q2 = asyncio.Queue()

    reader, writer = yield from asyncio.open_connection('192.168.1.103', 5555)
                # high-level call open streams - read and write

    print('Start')
    tasks = [
        asyncio.async(get_io_from_port(reader,q1)),
        asyncio.async(process_queue(q1, q2)),
        asyncio.async(put_io_to_port(writer, q2)),]


if __name__ == '__main__':
    loop = asyncio.get_event_loop()     # main loop declaration
    loop.run_until_complete(main())
+5
import queue
import asyncio

@asyncio.coroutine
def process_queue(q1, q2):
    while True:
        tmp = yield from q1.get()
        if tmp == 'ABCDEF':
            yield from q2.put('12345')
        elif tmp == 'GHIJKL':
            yield from q2.put('67890')
        else:
            print('There is a data error')

@asyncio.coroutine
def put_io_to_port(writer, q2):
    while True:
        try:
            data = yield from q2.get()
            writer.write(data)
        except IOError as e:
            print('OUT Port issue: ', e)

@asyncio.coroutine
def get_io_from_port(reader, q1):
    while True:
        try:
            data_i = yield from reader.read(1200)
            yield from q1.put(data_i)
        except IOError as e:
            print('IN Port issue: ', e)

@asyncio.coroutine
def main():
    q1 = asyncio.Queue()
    q2 = asyncio.Queue()

    reader, writer = yield from asyncio.open_connection('192.168.1.103', 5555)
                # high-level call open streams - read and write

    print('Start')

    asyncio.async(get_io_from_port(reader,q1))  # changed items so not
    asyncio.async(process_queue(q1, q2))        # in task list otherwise
    asyncio.async(put_io_to_port(writer, q2))   # they are not visible


if __name__ == '__main__':
    loop = asyncio.get_event_loop()     # main loop declaration
    loop.run_until_complete(main())

, .

0

All Articles