flask 启动线程实现异步调用

文章讲述了在Flask应用中如何避免直接使用`app`导致的重复引用错误,通过使用`current_app._get_current_object()`获取当前应用对象并作为参数传递给异步函数run_pssh,确保在函数内部正确使用context上下文处理并发问题。
摘要由CSDN通过智能技术生成
from flask import current_app

app= current_app._get_current_object()
thr = Thread(target=run_pssh, args=(app,ip_list, username, password, oss_path, remote_path, id_list, script_type))
    thr.start()


def run_pssh(app,ips, username, password, oss_path, remote_path, id_list,script_type):
    with app.app_context():

在我的接口中如果直接用app会报重复引用,必须使用

app= current_app._get_current_object()

然后再我的函数中添加一个参数app 把app加进去,并在函数中用context上下文。
就实现异步了,这里使用进程会报错。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值