Hi,

Here is a very basic example to show how to use start_server().
echo_server() coroutines will run in parallel.
---
import asyncio

@asyncio.coroutine
def echo_server(reader, writer):
    line = yield from reader.readline()
    print("Reply %r" % line)
    writer.write(line)
    yield from writer.drain()
    writer.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.start_server(echo_server, port=8000))
loop.run_forever()
loop.close()
---

You may add something like that to the documentation.

Victor

2014-07-18 10:47 GMT+02:00 yi huang <[email protected]>:
> The example in asyncio documentation only shows how to write echo server in
> twisted style, and i wonder how can i write server handler as a coroutine
> like in gevent, something like:
>
> def handle_client(transport):
>     while True:
>         buf = yield from transport.read(4096)
>         # handle request
>
>         # read some result from database without blocking other coroutine.
>         result = yield from block_read_from_database()
>
> loop.create_server(handle_client, '127.0.0.1', 3000)
>
>
> Thanks.
>
> --
> http://yi-programmer.com/

Reply via email to