Celery with Multiprocessing and SQLAlchemy
celeryでMultiprocessingをするときにどういう挙動をするのか気になったので,調査.
- 1. 普通にMultiprocessing
- 2. SQLAlchemyでsessionを作ってから,Multiprocessing
を検証する.
普通にMultiprocessing
AssertionError: daemonic processes are not allowed to have children
がでる
github issueによると,
$ export PYTHONOPTIMIZE=1
で取り敢えず直る.PYTHONOPTIMIZEは,disable Assertのoptimizationらしい.
でているエラーは,AssertionErrorなので,取り敢えず,抑えられた.supervisordで管理しているときはどうなんだろう(未調査)?
SQLAlchemyでsessionを作ってから,Multiprocessing
The above Session is associated with our SQLite-enabled Engine, but it hasn’t opened any connections yet. When it’s first used, it retrieves a connection from a pool of connections maintained by the Engine, and holds onto it until we commit all changes and/or close the session object.
のように,connection poolを持っているようだが.forkする前に
session = Session()
しても特に問題なかったよう.逆に,workerの中で
session = Session()
をするとWorker分しか,recordができないのでconnectionが切れている様に感じる.
なので,forkする前にsession = Session()しておくこと.
参考