1.一般好的解决方法
您应该使用消息队列来解耦
例如,
>主程序,创建任务,推送到queue_1
>多个广告系列工作人员从queue_1获取任务,处理并将一些多广告任务推送到queue_2
>多广告工作者从queue_2获取任务,处理完成.
逻辑简单,易于自己实现.还有一些现有的libs用于此类东西,例如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)
我在一些演示应用程序中使用此代码,但我不建议在生产中使用.