1.一般的な良い回避策
あなたは、
、例えば
を分離するために、メインプログラムをメッセージ・キューを使用してタスクを作成するにプッシュする必要がありますqueue_1
マルチキャンペーンワーカーgetタスクをqueue_1から処理して、一部をmulti-ad task
からqueue_2に転送します。
複数の広告ワーカーは、queue_2、process、doneからタスクを取得します。
ロジックは簡単で、自分で実装するのが簡単です。また、そのようなもののためのいくつかの既存のライブラリ、例えば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)
私はいくつかのデモアプリケーションでこのコードを使用しますが、私は生産に使用することはお勧めしません。