2 कार्यों: पहले एक, def_a
, एक अतुल्यकालिक समारोह है और दूसरा एक def_b
एक नियमित कार्य है और add_done_callback
समारोह के साथ एक कॉलबैक के रूप में def_a
के परिणाम के साथ कहा जाता है जो है।अजगर asyncio add_done_callback साथ async डीईएफ़
मेरे कोड इस तरह दिखता है:
import asyncio
def def_b(result):
next_number = result.result()
# some work on the next_number
print(next_number + 1)
async def def_a(number):
await some_async_work(number)
return number + 1
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(def_a(1))
task.add_done_callback(def_b)
response = loop.run_until_complete(task)
loop.close()
और यह पूरी तरह से काम है।
समस्या तब शुरू हुई जब दूसरा फ़ंक्शन, def_b
, असीमित हो गया। अब यह इस तरह दिखता है:
async def def_b(result):
next_number = result.result()
# some asynchronous work on the next_number
print(next_number + 1)
लेकिन अब मैं add_done_callback
कार्य करने के लिए यह प्रदान नहीं कर सकते, क्योंकि यह एक नियमित रूप से समारोह में नहीं है।
मेरा प्रश्न है- क्या यह संभव है और def_b
फ़ंक्शन पर def_b
असीमित है, तो मैं def_b
कैसे प्रदान कर सकता हूं?
उत्सुकता से साझा करने के लिए धन्यवाद, मेरी_callback – laycat
'sure_future (coro)' केवल queues 'coro()' के बाद निष्पादन के लिए प्रतीक्षा करें my_coro का उपयोग करके भविष्य को सुनिश्चित करने का लाभ क्या है और तुरंत 'coro' शुरू नहीं होता है() '। – Udi