🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

पायथन एसिंकियो पूर्ण गाइड 2026: एसिंक/प्रतीक्षा, कार्य और वास्तविक पैटर्न

⏱️2 min read  ·  336 words
Python asyncio Complete Guide 2026: Async/Await, Tasks & Real Patterns

पायथन काअसिन्सिओमॉड्यूल एसिंक/प्रतीक्षा सिंटैक्स का उपयोग करके समवर्ती कोड लिखने में सक्षम बनाता है। 2026 में, एसिंकियो फास्टएपीआई, स्टारलेट और एआईओएचटीटीपी को शक्ति प्रदान करता है। यह मार्गदर्शिका इवेंट लूप से लेकर वास्तविक दुनिया के पैटर्न तक सब कुछ कवर करती है।

एसिंसियो क्या है?

समवर्ती कार्यों को प्रबंधित करने के लिए asyncio एकल-थ्रेडेड ईवेंट लूप का उपयोग करता है – नेटवर्क I/O और API के लिए बिल्कुल सही जहां आप अधिकांश समय प्रतीक्षा में बिताते हैं, कंप्यूटिंग में नहीं।

import asyncio

async def main():
    print('Hello asyncio!')
    await asyncio.sleep(1)
    print('Done!')

asyncio.run(main())

async/प्रतीक्षा सिंटेक्स

|||| का प्रयोग करें एक कोरआउटिन को परिभाषित करने के लिए।async defका प्रयोग करें परिणाम तैयार होने तक रुकना।await📋कॉपी

import asyncio

async def fetch_data(url: str) -> str:
    await asyncio.sleep(0.5)  # simulate network
    return f'Data from {url}'

async def main():
    result = await fetch_data('https://api.example.com')
    print(result)

asyncio.run(main())

एकाधिक कोरआउटिन एक साथ चलाता है। 1-सेकंड के तीन कार्य कुल मिलाकर ~1 सेकंड में ख़त्म होते हैं, 3 नहीं।

asyncio.gather()📋कॉपी

import asyncio, time

async def task(name: str, delay: float):
    print(f'Start {name}')
    await asyncio.sleep(delay)
    return name

async def main():
    start = time.time()
    results = await asyncio.gather(
        task('A', 1.0),
        task('B', 2.0),
        task('C', 0.5),
    )
    print(f'Done in {time.time()-start:.2f}s: {results}')
    # ~2.0s not 3.5s

asyncio.run(main())

100 यूआरएल एक साथ प्राप्त करें, लगभग एक ही समय में।

📋कॉपी

import asyncio, aiohttp

async def fetch(session, url):
    async with session.get(url) as resp:
        return await resp.text()

async def main():
    urls = ['https://httpbin.org/delay/1'] * 5
    async with aiohttp.ClientSession() as session:
        results = await asyncio.gather(*[fetch(session, u) for u in urls])
    print(f'Fetched {len(results)} pages')

asyncio.run(main())

📋कॉपी

import asyncio

async def producer(q: asyncio.Queue):
    for i in range(5):
        await q.put(f'item-{i}')
        await asyncio.sleep(0.2)
    await q.put(None)  # sentinel

async def consumer(q: asyncio.Queue):
    while True:
        item = await q.get()
        if item is None: break
        print(f'Consumed {item}')

async def main():
    q = asyncio.Queue(maxsize=3)
    await asyncio.gather(producer(q), consumer(q))

asyncio.run(main())

📋कॉपी

import asyncio

async def risky(n):
    if n == 2: raise ValueError(f'Task {n} failed!')
    return f'Task {n} OK'

async def main():
    results = await asyncio.gather(
        *[risky(i) for i in range(4)],
        return_exceptions=True
    )
    for r in results:
        print('Error:', r if isinstance(r, Exception) else r)

asyncio.run(main())

कभी भी

  • का उपयोग न करें — उपयोगtime.sleep()कोरटाइन के अंदर I/O को ब्लॉक करने से बचेंawait asyncio.sleep()
  • |||| का प्रयोग करें प्रवेश बिंदु के रूप में
  • |||| का प्रयोग करें (पायथन 3.11+) संरचित समवर्ती के लिएasyncio.run()|||| के साथ डिबग करें निष्कर्ष
  • asyncio, Node.js-शैली समवर्तीता के लिए Python का उत्तर है। मास्टरasyncio.TaskGroup, जरूरत पड़ने पर कतारें जोड़ें, और क्लीनर त्रुटि सीमाओं के लिए टास्कग्रुप पर जाएं। एपीआई, स्क्रेपर्स और माइक्रोसर्विसेज के लिए बिल्कुल सही।
  • Debug withPYTHONASYNCIODEBUG=1

Conclusion

asyncio is Python’s answer to Node.js-style concurrency. Masterasyncio.gather(), add queues when needed, and move to TaskGroup for cleaner error boundaries. Perfect for APIs, scrapers, and microservices.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা