繁体   English   中英

异步调用模块[重复]

[英]Call modules asynchronously [duplicate]

我有一个调用其他四个模块的 Python 模块。 我想同时启动它们,让它们独立执行。

然而,目前,该模块正在同步/顺序执行其他模块。

这是代码。


    import asyncio
    import time

    from module_01 import main_01
    from module_02 import main_02
    from module_03 import main_03
    from module_04 import main_04

    async def say_after(delay, what):
        print(f"Time now {time.strftime('%X')}")                
        await asyncio.sleep(delay)
        what()
        print(f"Exit time {time.strftime('%X')}")     
       
    async def main():
        print(f"Process started at {time.strftime('%X')}")    
        task1 = asyncio.create_task(say_after(0, main_01))
        task2 = asyncio.create_task(say_after(0, main_02))
        task3 = asyncio.create_task(say_after(0, main_03))
        task4 = asyncio.create_task(say_after(0, main_04))
        await task1
        await task2
        await task3
        await task4

        print(f"Process completed at {time.strftime('%X')}")   
 
    asyncio.run(main())

以下是结果。

|Message|My Comment|
|--------|----------|
|Process started at 18:41:49||
|Time now 18:41:49||
|Time now 18:41:49||
|Time now 18:41:49||
|Time now 18:41:49||
|Module_01 completed.||
|Exit time 18:41:50|As expected.|
|Module_02 completed.||
|Exit time 18:41:52|I want this to be 18:41:51.|
|Module_03 completed.||
|Exit time 18:41:55|I want this to be 18:41:52.|
|Module_04 completed.|
|Exit time 18:41:59|I want this to be 18:41:53.|
|Process completed at 18:41:59|Looks like the other modules are executed sequentially which I do not want.|

作为代表,我在下面给出其他四个模块中的两个。

第一个模块


    from time import sleep
    
    def main_01():
        sleep(1)
        print ('Module_01 completed.')
    
    #main_01()

第四个模块。


    from time import sleep
    
    def main_04():  
        sleep (4)     
        print ('Module_04 completed.')
        
    
    #main_04()

有趣的是,另一个类似的代码示例给出了预期的结果。

下面的示例代码


    import asyncio
    import time
    
    async def say_after(delay, what):
        print(f"Time now {time.strftime('%X')}")                    
        await asyncio.sleep(delay)
        print(what)
        print(f"Exit time {time.strftime('%X')}")                
    
    async def main():
        print(f"Process started at {time.strftime('%X')}")    
        task1 = asyncio.create_task(say_after(1, 'hey'))
        task2 = asyncio.create_task(say_after(2, 'there,'))
        task3 = asyncio.create_task(say_after(3, 'what\'s'))
        task4 = asyncio.create_task(say_after(4, 'up?'))

        # Wait until tasks are completed (should take
        # around 4 seconds.)

        await task1
        await task2
        await task3
        await task4
        print(f"finished at {time.strftime('%X')}")

    asyncio.run(main())   

这是结果。

|Message|
|----------|
|Process started at 18:56:32|
|Time now 18:56:32|
|Time now 18:56:32|
|Time now 18:56:32|
|Time now 18:56:32|
|hey|
|Exit time 18:56:33|
|there,|
|Exit time 18:56:34|
|what's|
|Exit time 18:56:35|
|up?|
|Exit time 18:56:36|
|finished at 18:56:36|

这个例子告诉我 say_after function 是异步执行的,整个模块在 4 秒后完成。

我知道这两个模块是不同的,我想给出预期结果(4 秒总执行时间)的模块调用其他 Python 模块。 这是这里的问题吗? 如果不是,请问问题出在哪里?

谢谢你。

问候

马诺伊。

发布以将问题标记为已回答。


    await asyncio.sleep(1)

帮助解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM