2016-03-21 8:22 GMT+01:00 Bright Pan <
losts...@gmail.com>:
> the function a() must be not coroutine,
> How can i wait for the task until complete?
It's doesn't work well to mix blocking and non-blocking code. I
suggest to convert a() to non-blocking code using @corouting and yield
from/await. If you really want to mix blocking and non-blocking code,
you have two major design choices:
(1) the event loop is the "leader", it schedules blocking tasks using
loop.run_in_executor(): blocking tasks is quite "short" and produces a
result
(2) the blocking code is the "leader", it gives work to the event
loop, and then retrieves results from the event loop
The design (1) fits well into the asyncio design.
The design (2) helps to migrate an existing applications completly
based on blocking code ("regular" Python code), but wants to run some
tasks faster (ex: run multiple network queries in parallel).
For (2), a simple option is to use loop.run_until_complete(). You
schedule many tasks, and then use a simple task to collect all
requests "at once". loop.run_until_complete() blocks the current
thread. In short, you don't run blocking and non-blocking code in
parallel, both modes are exclusive and use the same thread.
A more complex design for (2) is to use two threads: run the event
loop in one thread, and blocking code in a different thread. The
classical option to exchange data (tasks) between the two threads is
to use a queue. For example, the blocking code schedules asynchronous
tasks in the event loop, and then polls the queue to retrieve result.
Victor