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

[Python] asynchronous programming -- high concurrency

編輯:Python

Catalog

    • Preface : Asynchronous programming asyncio What problems can be solved ?
      • 1、asyncio Introduce
      • 2、 Example

Preface : Asynchronous programming asyncio What problems can be solved ?

python because GIL( Global lock ) The existence of , Can't take advantage of multi-core , Performance has been criticized .

Solution 1 :

  • use multiprocessing replace Thread

    multiprocessing The emergence of the library is largely to make up for thread Cuz GIL And inefficiency . It makes a complete copy of thread The interface provided is easy to migrate . The only difference is that it uses multiple processes instead of multiple threads . Each process has its own independent GIL, So there will be no inter process GIL Scramble for .

    multiprocessing Not everything . Its introduction will increase the difficulty of data communication and synchronization between threads in program implementation . Take the counter for example , If we want multiple threads to accumulate the same variable , about thread Come on , Make a statement global Variable can . and multiprocessing Because processes cannot access each other's data , Only one... Can be declared in the main thread Queue,put Again get Or use share memory Methods . This extra cost of implementation makes it very painful to code multithreaded programs , Become more painful .

Solution 2 :

  • Asynchronous processing
    stay IO In dense network programming , Asynchronous processing is hundreds of times more efficient than synchronous processing , Make up for python Short board in terms of performance , Such as the latest microservice framework japronto,resquests per second Up to a million .
    python Another advantage is the library ( Third party Library ) Very rich , Very convenient to use .asyncio yes python3.4 Version to standard library ,python3.5 Joined again async/await characteristic .

Python Multithreading in multi-core CPU On , Only for IO Intensive computing has a positive effect ; And when there is at least one CPU Dense threads exist , So many threads are efficient because GIL And a big drop .

1、asyncio Introduce

Official documents :
asyncio Official documents .

git Warehouse :
asyncio git Warehouse .

asyncio It's for writing Concurrent Code library , Use async/await grammar .

asyncio Used to provide high performance for multiple Python The foundation of asynchronous framework , Including Internet and web services , Database connection Library , Distributed task queues and so on .

asyncio It's often about building IO Intensive and high-level structured The best choice of network code .

asyncio Provide a set of High level API be used for :

  • Concurrently function Python coroutines And realize complete control over its execution process ;

  • perform The Internet IO and IPC;

  • control Subprocesses ;

  • adopt queue Implement distributed tasks ;

  • Sync Concurrent code ;

Besides , Some more Lower level API To support the Developers of libraries and frameworks Realization :

  • Create and manage The event loop , To provide asynchronous API be used for Networking , function Subprocesses , Handle OS The signal wait ;

  • Use transports Achieve efficient protocols ;

  • adopt async/await grammar The bridge Callback based libraries and code .

2、 Example

coroutines
adopt async/await Syntax to declare , It's writing asyncio Recommended way to apply . for example , The following code snippet ( need Python 3.7+) prints “hello”, wait for 5 second , Re print “world”:

import asyncio
import time
async def main():
print('hello')
print(f"started at {time.strftime('%X')}")
await asyncio.sleep(5)
print(f"end at {time.strftime('%X')}")
print('world')
asyncio.run(main())

asynchronous http service :

import time
import aiohttp,asyncio
async def request_demo():
async with aiohttp.ClientSession() as session:
async with session.get('http://www.baidu.com/',verify_ssl=False) as response:
print('status:',response.status)
print('content-type',response.headers['content-type'])
html=await response.text()
print(f'body:{html[:15]}')
# Create an event loop
loop=asyncio.get_event_loop()
tasks=[request_demo(),]
loop.run_until_complete(asyncio.wait(tasks))

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