वांछित प्रभाव प्राप्त करने के लिए मैं क्या लिख ​​सकता हूं?

वांछित छपाई: 1 (झपकी फंक यहां शुरू हुई) 2 (99 यहां लौटा) 3

import asyncio

async def nap():
    await asyncio.sleep(4)
    print(3)

async def main():
    print(1)
    
    #I don't want to wait for func nap to complete. I just want nap to start here and finish the sleep after '2' is printed. Is that possible?
    await nap()
    print(2)
    return 99

# Python 3.7+
asyncio.run(main())

वर्तमान में प्रिंट: 1 (यहां झपकी लेना शुरू हुआ) 3 2 (99 यहां लौटा)

0
Morris 6 नवम्बर 2020, 13:22

1 उत्तर

सबसे बढ़िया उत्तर
import time
from concurrent.futures import ThreadPoolExecutor

def nap():
    print('Nap started...')
    time.sleep(4)
    print(3)

def main():
    print(1)
    executor = ThreadPoolExecutor(max_workers=100)
    task1 = executor.submit(nap)
    print(2)
    return 99

if __name__ == '__main__':
    data_returned_before_tasks_complete = main()
    print('data_returned_before_tasks_complete: ', data_returned_before_tasks_complete)
0
Morris 9 नवम्बर 2020, 09:45