当前位置: 首页 > 知识库问答 >
问题:

Asyncio与另一个协同程序同时运行Dash(Flask)服务器

焦兴为
2023-03-14

我创建了一个dash应用程序来显示另一个代码正在收集的信息,我希望使用Python中的asyncio模块同时运行这两个应用程序。

我的代码使用异步函数,Dash应用程序(基于Flask)在服务时阻止其他任何东西执行。

我不确定这是否需要打开更多的线程。

这是我当前的代码,它只运行主协程。

async def main():
    some code here...

    while True:
        try:
            await client.handle_message()
        except ConnectionClosedError as error:
            logger.error(error)
    
        for strategy in strategies:
            await asyncio.create_task(...)
            some code here...

async def run_dashboard():
    app = create_app()
    app.run_server('0.0.0.0', 5000, debug=False)


if __name__ == '__main__':
    some code here...

    # Currently just runs the main coroutine
    asyncio.run(main())

如何同时运行main和_dashboard?

共有3个答案

穆宾白
2023-03-14

在后台线程中运行Run_dashboard。参考文件。

async def run():
    await asyncio.gather(
        asyncio.to_thread(run_dashboard),
        main()
    )

asyncio.run(run())

请注意,asyncio.to_thread是3.9版中的一个新函数。对于早于3.9的python版本,请复制以下代码threads.py。

周志文
2023-03-14

如果您真的希望它在一个进程中运行,以下方法对我有效:

from functools import partial
from threading import Thread    


partial_run = partial(app.run, host="0.0.0.0", port=5000, debug=True, use_reloader=False)
t = Thread(target=partial_run)
t.start()
asyncio.run(main())
司马念
2023-03-14

坦率地说,将DASH(烧瓶)与一些异步工作结合在一个过程中是不好的设计,考虑在不同的进程(即应用程序)中运行烧瓶和异步活动。

尽管如此,如果你仍然想在一个进程中运行所有的程序,我可以给你下面的工作示例,请按照评论并询问你是否有任何问题:

from flask import Flask, jsonify
import asyncio
from threading import Thread

# ** Async Part **


async def some_print_task():
    """Some async function"""
    while True:
        await asyncio.sleep(2)
        print("Some Task")


async def another_task():
    """Another async function"""
    while True:
        await asyncio.sleep(3)
        print("Another Task")


async def async_main():
    """Main async function"""
    await asyncio.gather(some_print_task(), another_task())


def async_main_wrapper():
    """Not async Wrapper around async_main to run it as target function of Thread"""
    asyncio.run(async_main())

# *** Flask Part ***:


app = Flask(__name__)


@app.route("/", methods=["GET"])
def index():
    """just some function"""
    return jsonify({"hello": "world"})


if __name__ == '__main__':
    # run all async stuff in another thread
    th = Thread(target=async_main_wrapper)
    th.start()
    # run Flask server
    app.run(host="0.0.0.0", port=9999)
    th.join()
 类似资料:
  • 问题内容: 我正在使用python更新一些LED。我一直在这样做: 我想用Flask充当一些我可以在我的浏览器中运行的ReactJS前端(以更改当前模式等)与Python中的LED控制代码之间的桥梁。 我的Flask工作正常,可以处理HTTP请求,等等。我想知道如何设置我的flask应用程序同时连续运行(或快速运行),同时仍然能够相互通信,就像这样: 我遇到了,这似乎可以解决问题,但对于我的问题有

  • 问题内容: 我正在尝试围绕我的烧瓶应用程序为办公室中的菜鸟创建一个简单的tkinter GUI窗口。我希望脚本按以下顺序执行这些任务: 启动Flask Web服务器 用一个按钮打开一个tkinter GUI窗口。当按下时,该按钮将打开应用程序的索引页面(例如http://127.0.0.1:5000) 关闭tkinter gui窗口时终止flask Web服务器 到目前为止,这是我所拥有的,但是该

  • 问题内容: 在go程序中,我想同时运行两个Web服务器, 显然它们将在两个不同的端口(如果需要,还有IP地址)上提供服务, 问题在于调用,当我尝试为’ /’对于第二台服务器,它会慌张,并说已经有一个与’/’相关联的处理程序, 我想我除了需要之外还需要创建一个多路复用器,我尝试使用来实现,但无法弄清楚, 在同一程序/进程中运行两个Web服务器是否存在根本性的错误。 更清楚地说,这两个Web服务器之一

  • 我试图在修改给定模板后运行Corda应用程序。在运行节点之后,我尝试运行一个模板服务器。以下是详细信息 Corda版本:4.0 在端口8161上运行的ActiveMQ 在运行上述任务后,我得到以下异常。我看了笔记,把Corda升级到v4。0,但仍然无法运行服务器。 我尝试过: > 在一个github问题中指定将corda升级到v4.0 添加jolokia作为jvm代理 什么都没用。谁能帮忙吗?

  • 问题内容: 如果我有另一个正在运行的apache / web-server实例,我用所有这些命令痛苦地分析了昨天的全部内容 我没有在端口8080上运行的Apache或任何其他服务器的另一个实例。 但是,XAMPP给了我这个: 我该怎么办? 我也将httpd.conf编辑为LISTEN到端口9876,并且仍然相同。 问题答案: 如果: 然后,也许有一个锁定文件位于启动要检查的地方?这些通常在/ va

  • 如果我有另一个apache/web服务器实例在运行,我已经痛苦地分析了昨天的所有内容,包括所有这些命令 我没有在端口8080上运行的Apache或任何其他服务器的另一个实例。 然而,XAMPP给了我这样的信息: 我该怎么办? 我还编辑了httpd.conf监听端口9876,仍然是一样的。