Python 不执行在apply_async中更新数据库模型
我正在尝试使用pool.apply\u async发出非阻塞http请求,在得到响应后,使用新值更新现有的db raw。在我尝试更新值之前,一切都正常,我收到错误“找不到应用程序”。在视图函数内部工作或推送应用程序上下文'。你能帮我理解为什么吗 models.pyPython 不执行在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
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)
,打印错误