繁体   English   中英

如何从另一个函数进行异步函数调用?

[英]How do I make an asynchronous function call from another function?

目标是在执行程序其余部分的过程中创建一个 docx 文档。

“第一个”函数应该只调用将创建 docx 的异步“第二个”。 现在我使用模块 asyncio、multiprocessing、concurrent.futures,但不创建 docx:

def first(self, event):
    pool = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count())
    loop = asyncio.get_event_loop()
    loop.run_in_executor(pool, self.second)

async def second(self):
    document = Document()
    document.save('test.docx')

我确定问题出在“第一个”函数上,它调用“第二个”的方式,但是一个人对我说,这不是异步的错。 在我找到最接近解决方案的方法之前,我一直面临着只有在整个程序执行过程完成后才创建文档的问题——这不是目标。

我正在做一个没有时间修复的旧项目; 里面的基本东西有很多错误,所以浏览器没有帮助——它需要一些针对具体情况的东西。 即便如此,请告诉我如何解决问题。 谢谢。

无需创建second异步。 我假设您可以将其更改为常规功能。

您可能只想在后台操作系统线程中开始文件创建:

def first():
    with ThreadPoolExecutor(max_workers=1) as executor:
        fut = executor.submit(second)  # start `second` in background

        # rest of the program

        fut.result()  # make sure `second` is finished


def second():
    document = Document()
    document.save('test.docx')

如果瓶颈是磁盘 I/O,这应该可以解决问题。 如果瓶颈是 CPU,您应该考虑使用ProcessPoolExecutor而不是ThreadPoolExecutor


这是可重现的代码:

import time
from concurrent.futures import ThreadPoolExecutor


def first():
    with ThreadPoolExecutor(max_workers=1) as executor:
        fut = executor.submit(second)  # start `second` in background

        print('Rest of the program started')
        time.sleep(2)  # rest of the program
        print('Rest of the program finished')

        fut.result()  # make sure `second` is finished


def second():
    time.sleep(1)  # create doc
    print('Doc created')


first()

暂无
暂无

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

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