PYTHON_REAL Telegram 929
Параллелизм, конкурентность и 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
👍3



tgoop.com/python_real/929
Create:
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

View MORE
Open in Telegram


Telegram News

Date: |

Healing through screaming therapy The court said the defendant had also incited people to commit public nuisance, with messages calling on them to take part in rallies and demonstrations including at Hong Kong International Airport, to block roads and to paralyse the public transportation system. Various forms of protest promoted on the messaging platform included general strikes, lunchtime protests and silent sit-ins. fire bomb molotov November 18 Dylan Hollingsworth yau ma tei On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." Private channels are only accessible to subscribers and don’t appear in public searches. To join a private channel, you need to receive a link from the owner (administrator). A private channel is an excellent solution for companies and teams. You can also use this type of channel to write down personal notes, reflections, etc. By the way, you can make your private channel public at any moment.
from us


Telegram Реальный Python
FROM American