如何避免使用芹菜进行分类副作用?
我正在使用芹菜运行一个基于类的应用程序,但是我注意到,当两个过程同时运行时,同类中的某些静态方法并非独立起作用。这是我的SIM应用程序中的应用程序调用
import os
from PriceOptimization.celery import app
from .Tasks_Sim.sim import Sim, final_report
@app.task(name='Simulations.tasks.scoring')
def simulation(clients, deciles):
s = Sim(**sim_params)
market_by_year = s.control_flow(my_save_path)
report = final_report(market_by_year)
return report
,我有一个类方法为我的实例创建ID,如下所示
class Company:
company_id = 0
@classmethod
def set_company_no(cls):
cls.company_id += 1
return cls.company_id-1
def __init__(self, companies, year):
self._company_id = Company.set_company_no()
self._company_year = year
。错误向我表明我的工人不是独立的,并且我的company_id
对象不会随着下一个调用从零开始。如何防止此副作用并独立运行每个应用程序?
I am running a class based app using celery, but I am noting that when two processes run simultaneously, certain staticmethods in the class are not acting independently. Here is the app invocation
import os
from PriceOptimization.celery import app
from .Tasks_Sim.sim import Sim, final_report
@app.task(name='Simulations.tasks.scoring')
def simulation(clients, deciles):
s = Sim(**sim_params)
market_by_year = s.control_flow(my_save_path)
report = final_report(market_by_year)
return report
Within my Sim app, I have a class method that creates id's for my instance as follows
class Company:
company_id = 0
@classmethod
def set_company_no(cls):
cls.company_id += 1
return cls.company_id-1
def __init__(self, companies, year):
self._company_id = Company.set_company_no()
self._company_year = year
Usually the first task instantiated will complete successfully, but on the next invocation, I am getting a list index out of range error that suggests to me that my workers are not independent and that my company_id
object is not commencing from zero with the next invocation. How can I prevent this side effect and have each app run independently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,我选择使用redis锁定依次运行我的过程:
For now, I have elected to make my process run sequentially using a redis lock: