Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不执行在apply_async中更新数据库模型_Python_Flask - Fatal编程技术网

Python 不执行在apply_async中更新数据库模型

Python 不执行在apply_async中更新数据库模型,python,flask,Python,Flask,我正在尝试使用pool.apply\u async发出非阻塞http请求,在得到响应后,使用新值更新现有的db raw。在我尝试更新值之前,一切都正常,我收到错误“找不到应用程序”。在视图函数内部工作或推送应用程序上下文'。你能帮我理解为什么吗 models.py class Proxy(db.Model): __tablename__ = 'Proxy' __table_args__ = {'extend_existing': True} id = Column(In

我正在尝试使用pool.apply\u async发出非阻塞http请求,在得到响应后,使用新值更新现有的db raw。在我尝试更新值之前,一切都正常,我收到错误“找不到应用程序”。在视图函数内部工作或推送应用程序上下文'。你能帮我理解为什么吗

models.py

class Proxy(db.Model):
    __tablename__ = 'Proxy'
    __table_args__ = {'extend_existing': True}

    id = Column(Integer, primary_key=True, autoincrement=True)
    ip_address = Column(db.String(20))
    port_number = Column(db.String(7))
    proxy_type = Column(db.String(7))
    username = Column(String(100))
    password = Column(String(100))
    status = Column(Integer, default=0)
    user_id = Column(Integer, ForeignKey('User.id'))
    proxies = relationship("User", backref=backref("Proxies", cascade="all,delete"))

    def __init__(self, **kwargs):
        db.Model.__init__(self, **kwargs)

    def __repr__(self):
        return '{}:{}@{}:{}'.format(self.username, self.password, self.ip_address, self.port_number)

    def __getitem__(self, key):
        return getattr(self, key)

    def __setitem__(self, key, value):
        if key in self:
            setattr(self, key, value)
路由器.py

from app.base.models import Proxy 
pool = Pool(10)
proxy = ProxyModel.query.join(UserModel).filter(UserModel.id == current_user.id).filter(ProxyModel.id == proxy_id).first()

pool.apply_async(long_running_job, args=[proxy])

def long_running_job(proxy):
    proxies = {
        "http": "socks5h://" + str(proxy) if proxy.proxy_type == 'socks5' else "http://" + str(proxy),
        "https": "socks5h://" + str(proxy) if proxy.proxy_type == 'socks5' else "https://" + str(proxy),
    }
    with suppress(Exception):
        response = requests.get('https://graph.facebook.com/', proxies=proxies, timeout=5)

        if response and 'Unsupported get request' in response.text:
            print('Valid')
            proxy.status = 1
            db.session.commit()
        else:
            print('INValid')
            proxy.status = -1
            db.session.commit()

失败的可能原因:

  • apply\u async
    要求args是元组。使用
    pool。改为应用异步(长时间运行的\u作业,args=(代理),)
  • 正如该方法所说,该操作是异步的,因此可以随时完成。您可以使用
    callback
    参数查看它何时完成,例如:
    callback=lambda x:print(“已完成任务”)
  • 发生了任何其他错误。要跟踪此错误,可以使用
    error\u callback
    参数,例如:
    error\u callback=lambda x:print(x)
    ,打印错误
  • 这些进程不共享内存。也许你正试图访问另一个进程中的变量,比如db。某些数据库连接无法在进程之间共享
  • 希望这些都有帮助