开发工具:
文件大小: 186kb
下载次数: 0
上传时间: 2019-07-03
详细说明:asyncio in python simple introduction for asyncio.Contents
1 Chapter 1: First steps with asyncio
1. 1 Why use asencio
1. 2 Getting Started
1. 3 Hello world
1. 4 Hello clock
1.5 Http client example
1.6 asyncio perfornance
34556
1.7 Learn asyncio if you come from Twisted
1. 8 Getting Help
·
8
2 Chapter 2: Advanced topics
2.1 TCP echo client and server
2.2 Threads
2.3 Subprocess
2.4 Producer/consumer
.12
2.5 Asyncio Debug Mode
...14
3 Chapter 3: Larger examples
15
3.1 Web Scraping
15
4 Indices and tables
29
4.1 Glossary
29
ee aso
6 Contributing
33
6.1 Asyncio documentation
33
6.2 Notes to writers
33
6.3 Ideas
6.4 How to install Sphinx
6.5 How to build the documentation
34
6.6 See also...,,,,,,,,.,,,,,,,
34
CHAPTER
Chapter 1: First steps with asyncio
1.1 Why use asyncio?
1.1.1 Why asynchronous programming?
asyncio is a library to write asynchronous applications. It is the most efficient way to implement a network server
having to handle many concurrent users
1.1.2 But gevent and eventlet just work
Or Why should i bother with all these extra annoying async and await keywords
In short, asyncio adopted a radically different solution for race conditions
Parallel computing using threads is hard because of race conditions. Gevent and eventlet have a similar issue using
green(lightweight)threads
Code written with asyncio is less error-prone: by just looking at the code, it is possible to identify which parts of the
code are under our controls and where the event loop takes over the control fow and is able to run other tasks when
our task is waiting for something.
gevent and eventlet are designed to hide the asynchronous programming. For non-expert, and sometimes even for
experts, it is really hard to guess where the event loop is allowed to suspend the task and run other tasks in background
It is even worse. A modification in a third party library can change the behaviour of our code, introduce a new point
where the task is suspended
For an example, see the Ca(shlche Coherent) Money"section of the Unyielding article(by Glyph, February, 2014)
Asyncio Documentation Documentation, Release 0.0
1.2 Getting Started
1.2.1 Python 3.5(or higher) only
This documentation is written for Python 3.5 to avail of the new a sync and await keywords
IfyouhavePython3.5installedyouonlyneedtoinstallaiohttp:
pipinstall-uaiohttp
If you don t have python 3. 5 installed yet, you have several options to install it
All platforms with conda
Download and install Miniconda for our platform
Create a new Python 3.5 environment(named aio35, use a different if you like
highlight: bash
conda create -n aio35 python=3.5
Activate it. Linux and os x
highlight:: bash
S source activate aio35
Windows
highlight:: bash
S source activate aio35
Installaiohttp
highlight:: bash
S(aio35) pip
install aiohtti
Platform specific
Windows: The easiest way to use Python 3.5 would be to use a package manager such as conda. See the
installation instructions above
Mac os X: Install Homebrew and then type brew install python
Linux: Ubuntu 16.04+ and Arch linux ship with Python 3.5 included. If you don t have Python 3.5+on your
computer, you can compile it or use Python
1.2.2 Create a virtual environment to run examples
If you dont use conda(see above), create a virtual environment
python -m venv venv
Chapter 1. Chapter 1: First steps with asyncio
Asyncio Documentation documentation Release 0.0
Note: Depending on your platform, the Python 3 interpreter could be invoked by python instead. This is the case
for conda on Windows for example
Installaiohttpinthevirtualenvironment
/venv/bin/python-mpipinstall-uaiohttp
1.3 Hello World
This is a series of examples showing the basics of how to write coroutines and schedule them in the asyncio event
1.3.1 Simple coroutine
This example uses the asyncio. AbstractEventLoop run_until_ complete() method to schedule a sim-
ple function that will wait one second, print hello and then finish
Because it is launched with asyncio. AbstractEventLoop. run_until_complete(), the event loop itself
will terminate once the coroutine is completed
import asencio
async def say (what, when)
await asyncio. sleep(when)
print(what)
1。op= asyrc1o.qet_even=_1。p()
loop. run_until_complete(say(hello world, 1)
loop. clcs(
1.3.2 Creating tasks
This second example shows how you can schedule multiple coroutines in the event loop, and then run the event loop
Notice that this example will print second_ hello before first _ hello, as the first task scheduled waits longer
that the second one before printing
Also note that this example will never terminate, as the loop is asked to run forever
import asyncio
async def say(what, when)
await asyncio sleep(when)
print(what)
loop
asencio. get_ even=_loop()
loop. create task(say('first hello',2)
loop. create task(say (' second hello, 1))
(continues on next page
1.3. Hello world
3
Asyncio Documentation documentation release 0.0
(continued from previous page)
loop.run_forever(
loop. close()
1.3.3 Stopping the loop
This third example adds another task that will stop the event loop before all scheduled tasks could execute, which
results in a warning
import asyncio
async def say (what, when):
await asyncio sleep(when)
print (what)
async def stop_after(loop, when)
await asyncio. sleep(when)
loop. stop()
loop= asyncio get even- loop(
oop create_task(say( first hello,2))
loop. create-task(say(' second hello', 1))
loop. create task(say( third hello, 4)
loop. crcatetask(stop _after(loop, 3))
f
loop. close()
Warning:
highlight:: none
Task was destroyed but it is pending! task: wait_ for=>
1. 4 Hello clock
Example illustrating how to schedule two coroutines to run concurrently. They run for ten minutes, during which the
first coroutine is scheduled to run every second, while the second is scheduled to run every minute
The function asyncio gather is used to schedule both coroutines at once
Import async1o
async def print_ second(
imprint seconds m
while true
f。 r 1 n range(50):
print (ir's)
await asyncio sleep(1)
(continues on next page)
Chapter 1. Chapter 1: First steps with asyncio
Asyncio Documentation documentation Release 0.0
(continued from previous page)
async def print_every minute():
for i in range(1r 10)
await asyncio sleep(60)
print(i, 'minuLe'
loop= asyncio get_even=_loop(
loop. run_untiIcomplete(
asyncio. gather(prin-_ second(),
p≌in= every minute()
loop. close()
1.5 Http client example
Http ClienT example
import asyncio
importaiohttp
async def fetch page(session, url):
withaiohttp.Timeou=(l0
async with session. get (url)as response:
assert response status ==200
return await response. read()
loop asyncio, get_even=_loop()
withaichttp.Clientsession(loop-loop)assession
conCerT
n unTil
lele
fetch_page(session,'http://python.org'))
print (content)
oop.C⊥cse()
Formoreinformationseeaiohttpdocumentation
1.6 asyncio performance
Random notes about tuning asyncio for performance. Performance means two different terms which might be incom-
patible
Number of concurrent requests per second
Request latency in seconds: min/average/max time to complete a request
1.6.1 Architecture: Worker processes
Because of its GIL, CPython is basically only able to use 1 CPU. To increase the number of concurrent requests, one
solution is to spawn multiple worker processes. See for example
Unicorn
API-Hour
1.5. Http client example
Asyncio Documentation documentation release 0.0
1.6.2 Stream limits
limit parameter of Stream Reader/open_connection
set write buffer limits( low/high water mark on writing for transports
iohttpusesset_writer_buffer_limits(0)forbackpressuresupportandimplementedtheirownbuffering
see
aio-libs/aiohttp#136
Some thoughts on asynchronous API design in a post-asyncawait world (November, 2016) by Nathaniel J
Smith
1.6.3 TCP NODELAY
Since Python 3.6, asyncio now sets the TCP_NODELAY option on newly created sockets: disable the Nagle algorithm
for send coalescing. Disable segment buffering so data can be sent out to peer as quickly as possible, so this is typically
used to improve network utilisation
See Nagle's algorithm
1.6.4 TCP QUICKACK
This option is not used by asyncio by default.
The TCP_QUICKACK option can be used to send out acknow ledgements as early as possible than delayed under some
protocol level exchanging, and it's not stable/permanent, subsequent TCP transactions(which may happen under the
hood) can disregard this option depending on actual protocol level processing or any actual disagreements between
user setting and stack behaviour
1.6.5 Tune the linux kernel
Linux TCP sysctl
/proc/sys/net/ipv4/tcp_mem
/proc/sys/net/core/rmem_default and/proc/sys/net/core/rmem_max: The default and
maximum amount for the receive socket memorv
/proc/sys/net/core/wmem_de fault and/proc/sys/net/ core/wmem_max: The default and
maximum amount for the send socket memor
/proc/sys/net/core/optmem_max: The maximum amount of option memory buffers
net. ipv4. tcp no metrics_save
net core netdev_max_ backlog: Set maximum number of packets, queued on the INPUT side, when
the interface receives packets faster than kernel can process them
1.7 Learn asyncio if you come from Twisted
The twisted project is probably one of the oldest libraries that supports asynchronous programming in Python. It has
been used by many programmers to develop a variety of applications. It supports many network protocols and can
be used for many different types of network prograMming. In fact, asyncio was heavily inspired by twisted. The
Chapter 1. Chapter 1: First steps with asyncio
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.