tgoop.com/python_real/929
Create:
Last Update:
Last Update:
Параллелизм, конкурентность и AsyncIO в Python — на примерах
Эта статья рассматривает, как ускорить операции, ограниченные процессором (CPU-bound) и вводом-выводом (IO-bound), с помощью многопроцессорности, многопоточности и AsyncIO.
# tasks.py
import os
from multiprocessing import current_process
from threading import current_thread
import requests
def make_request(num):
# io-bound
pid = os.getpid()
thread_name = current_thread().name
process_name = current_process().name
print(f"{pid} - {process_name} - {thread_name}")
requests.get("https://httpbin.org/ip")
async def make_request_async(num, client):
# io-bound
pid = os.getpid()
thread_name = current_thread().name
process_name = current_process().name
print(f"{pid} - {process_name} - {thread_name}")
await client.get("https://httpbin.org/ip")
def get_prime_numbers(num):
# cpu-bound
pid = os.getpid()
thread_name = current_thread().name
process_name = current_process().name
print(f"{pid} - {process_name} - {thread_name}")
numbers = []
prime = [True for i in range(num + 1)]
p = 2
while p * p <= num:
if prime[p]:
for i in range(p * 2, num + 1, p):
prime[i] = False
p += 1
prime[0] = False
prime[1] = False
for p in range(num + 1):
if prime[p]:
numbers.append(p)
return numbers
https://testdriven.io/blog/python-concurrency-parallelism/
#python
👉 @python_real
BY Реальный Python
Share with your friend now:
tgoop.com/python_real/929
