程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Initial experience of Python collaboration

編輯:Python

Preface

In understanding Python After multithreading and multiprocessing of concurrent programming , Let's take a look at the asyncio The asynchronous IO Programming => coroutines

coroutines

coroutines (Coroutine) Also called tasklet 、 fibers , A contract is not a process or thread , The execution process is similar to Python Function call ,Python Of asyncio Asynchrony of module implementation IO In the programming framework , A coroutine is a async Call of asynchronous function defined by keyword

A process contains multiple threads , Similar to a human tissue, there are many kinds of cells working , Again , A program can contain multiple collaborations . Multiple threads are relatively independent , Thread switching is controlled by the system . Again , Multiple processes are also relatively independent , But its switching is controlled by the program itself

What's the advantage of the project ?

  • Very efficient execution , Because subroutine switching ( function ) It's not thread switching , Controlled by the program itself , There is no cost of switching threads . So compared to multithreading , The more threads , The more obvious is the advantage of CO process performance
  • No multi-threaded locking mechanism is required , Because there's only one thread , There is no conflict between writing variables at the same time , There is no need to lock when controlling shared resources , So the execution is much more efficient

Python3.x coroutines

Python3.x It also provides the following ways to implement the cooperation process :asyncio + yield from (python3.4+) asyncio + async/await (python3.5+)

Python3.4 Later introduced asyncio modular , It can support the process very well

asyncio

evnt_loop: The event loop , It's like an infinite loop , We can register some functions on this event loop , When that happens , The corresponding processing method is called

coroutine: Chinese translation is called Xie Cheng , stay Python It is often referred to as the type of process object , We can register the process object in the time loop , It will be called by the event loop . We can use async Keyword to define a method , This method is not immediately executed when invoked , Instead, it returns a coroutine object

task: Mission , It is a further encapsulation of the coroutine object , Contains the various states of the task

future: Represents the outcome of future tasks performed or not performed , Actually and task There is no essential difference

async

Define asynchronous functions

async def async_function():
print('Number:', 1)
return 1
print(async_function())
Output :
Returns a coroutine object
<coroutine object async_function at 0x10a4b0f10>

call send Methods the activation

print(async_function().send(None))

An asynchronous function

async def async_function():
print('Number:', 1)
r = requests.get('http://www.baidu.com')
return r
coroutine = async_function()
loop = asyncio.get_event_loop()
t = loop.run_until_complete(coroutine)
print(t)
print('After calling loop')

An asynchronous function task

coroutine = async_function()
loop = asyncio.get_event_loop()
task = loop.create_task(coroutine)
print('Task:', task)
loop.run_until_complete(task)
print('Task:', task)
print('After calling loop')
Output : coroutine There are too many running states for objects , such as running、finished etc.
Task: <Task pending coro=<async_function() running at async_t.py:104>>
Number: 1
Task: <Task finished coro=<async_function() done, defined at async_t.py:104> result=<Response [200]>>
After calling loop

An asynchronous function ensure_future

task = asyncio.ensure_future(coroutine)
print('Task:', task)
loop = asyncio.get_event_loop()
loop.run_until_complete(task)
print('Task:', task)
print('After calling loop')

Asynchronous function binding callback

async def async_function():
print('Number:', 1)
r = requests.get('http://www.baidu.com')
return r
def callback(task):
print('Status:', task.result())
task = asyncio.ensure_future(coroutine)
task.add_done_callback(callback)
print('Task:', task)
loop = asyncio.get_event_loop()
loop.run_until_complete(task)
print('Task:', task)

Asynchronous function network concurrent execution

import requests_async as requests
async def post_requests():
print("Hello world!")
async with requests.Session() as session:
print("create Session statr")
response = await session.get('https://example.org')
print("create Session over")
print(response.status_code)
# print(response.text)
tasks = [post_requests() for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
print('Task:', tasks)

Asynchronous function network request

async def get(url):
return requests.get(url)
async def request():
url = 'http://127.0.0.1:5000/api'
print('Waiting for', url)
response = await get(url)
print('Get response from', url, 'Result:', response.text)
tasks = [asyncio.ensure_future(request()) for _ in range(5)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

Multitask asynchronous

import asyncio
import requests
async def request():
url = 'https://www.baidu.com'
status = requests.get(url)
return status
tasks = [asyncio.ensure_future(request()) for _ in range(5)]
print('Tasks:', tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
for task in tasks:
print('Task Result:', task.result())

aiohttp Asynchronous requests

async def aiohttp_get():
url = 'http://127.0.0.1:5000/api'
print('Waiting for', url)
session = aiohttp.ClientSession()
response = await session.get(url)
result = await response.text()
print('Get response from', url)
session.close()
return result
tasks = [asyncio.ensure_future(aiohttp_get()) for _ in range(5)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

ending

Above is python Some simple experiences of the collaboration process , In the follow-up, we will combine some actual cooperation projects , Share more tips ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved