LYTHON Telegram 57
برنامه‌نویسی غیرهمزمان (Asynchronous)

برنامه‌نویسی غیرهمزمان یکی از مفاهیم مهم در توسعه نرم‌افزار است که به شما امکان می‌دهد عملیات‌های مختلف را به طور همزمان و بدون مسدود کردن (Blocking) برنامه اجرا کنید. این روش به ویژه برای بهبود کارایی برنامه‌ها در زمان اجرای عملیات‌های I/O مثل درخواست‌های شبکه، خواندن و نوشتن فایل‌ها ودیگر فرایند ها مفید است.

◀️ مفاهیم کلیدی:

1. حلقه رویداد (Event Loop): هسته اصلی اجرای کدهای غیرهمزمان که مسئول مدیریت وظایف مختلف است.
2. کوروتین (Coroutine): نوع خاصی از تابع که می‌تواند متوقف و از سر گرفته شود. کوروتین‌ها با async def تعریف می‌شوند.
3. انتظار (Await): برای متوقف کردن اجرای یک کوروتین تا تکمیل شدن یک وظیفه دیگر استفاده می‌شود.
4. وظیفه (Task): یک کوروتین زمان‌بندی شده برای اجرا به طور همزمان با سایر وظایف.

◀️ مثال ساده:

import asyncio

async def say_hello():
print("hello!")
await asyncio.sleep(1) # delay simulation
print("world!")

asyncio.run(say_hello())


◀️ مثال پیشرفته‌تر:

import asyncio

async def fetch_data(name, delay):
print(f"started {name}")
await asyncio.sleep(delay)
print(f"ended {name}")
return f"result {name}"

async def main():
tasks = [
asyncio.create_task(fetch_data('data 1', 2)),
asyncio.create_task(fetch_data('data 2', 3)),
asyncio.create_task(fetch_data('data 3', 1)),
]
results = await asyncio.gather(*tasks)
print("results:", results)

asyncio.run(main())


@lython



tgoop.com/lython/57
Create:
Last Update:

برنامه‌نویسی غیرهمزمان (Asynchronous)

برنامه‌نویسی غیرهمزمان یکی از مفاهیم مهم در توسعه نرم‌افزار است که به شما امکان می‌دهد عملیات‌های مختلف را به طور همزمان و بدون مسدود کردن (Blocking) برنامه اجرا کنید. این روش به ویژه برای بهبود کارایی برنامه‌ها در زمان اجرای عملیات‌های I/O مثل درخواست‌های شبکه، خواندن و نوشتن فایل‌ها ودیگر فرایند ها مفید است.

◀️ مفاهیم کلیدی:

1. حلقه رویداد (Event Loop): هسته اصلی اجرای کدهای غیرهمزمان که مسئول مدیریت وظایف مختلف است.
2. کوروتین (Coroutine): نوع خاصی از تابع که می‌تواند متوقف و از سر گرفته شود. کوروتین‌ها با async def تعریف می‌شوند.
3. انتظار (Await): برای متوقف کردن اجرای یک کوروتین تا تکمیل شدن یک وظیفه دیگر استفاده می‌شود.
4. وظیفه (Task): یک کوروتین زمان‌بندی شده برای اجرا به طور همزمان با سایر وظایف.

◀️ مثال ساده:

import asyncio

async def say_hello():
print("hello!")
await asyncio.sleep(1) # delay simulation
print("world!")

asyncio.run(say_hello())


◀️ مثال پیشرفته‌تر:

import asyncio

async def fetch_data(name, delay):
print(f"started {name}")
await asyncio.sleep(delay)
print(f"ended {name}")
return f"result {name}"

async def main():
tasks = [
asyncio.create_task(fetch_data('data 1', 2)),
asyncio.create_task(fetch_data('data 2', 3)),
asyncio.create_task(fetch_data('data 3', 1)),
]
results = await asyncio.gather(*tasks)
print("results:", results)

asyncio.run(main())


@lython

BY روزمرگی های یک برنامه نویس


Share with your friend now:
tgoop.com/lython/57

View MORE
Open in Telegram


Telegram News

Date: |

The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians. Earlier, crypto enthusiasts had created a self-described “meme app” dubbed “gm” app wherein users would greet each other with “gm” or “good morning” messages. However, in September 2021, the gm app was down after a hacker reportedly gained access to the user data. Telegram users themselves will be able to flag and report potentially false content. Avoid compound hashtags that consist of several words. If you have a hashtag like #marketingnewsinusa, split it into smaller hashtags: “#marketing, #news, #usa. More>>
from us


Telegram روزمرگی های یک برنامه نویس
FROM American