1.一般好的工作
您应该使用消息队列解耦
例如,
主要程序,创建任务,推queue_1
multi-campaign worker get get来自队列_1的任务,处理并将一些multi-ad task
推送到队列_2
多广告工作者从队列2中获取任务,处理完成。
该逻辑很简单,易于自己实现。还有一些现有的库,如rq
/celery
。
2.简单的解决方法
如果得到AssertionError
,使用线程,而不是
def run_in_subprocess(func, *args, **kwargs):
from multiprocessing import Process
thread = Process(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
def run_in_thread(func, *args, **kwargs):
from threading import Thread
thread = Thread(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
def run_task(config):
try:
run_in_subprocess(xxxx_task, config)
except AssertionError:
print('daemonic processes are not allowed to have children, use thread')
run_in_thread(xxxx_task, config)
我使用此代码在一些演示应用程序,但我不建议生产使用。