Multiprocessing in Python while limiting the number of running processes

前端 未结 4 1704
半阙折子戏
半阙折子戏 2021-02-02 12:46

I\'d like to run multiple instances of program.py simultaneously, while limiting the number of instances running at the same time (e.g. to the number of CPU cores available on m

4条回答
  •  无人及你
    2021-02-02 13:06

    You should use a process supervisor. One approach would be using the API provided by Circus to do that "programatically", the documentation site is now offline but I think its just a temporary problem, anyway, you can use the Circus to handle this. Another approach would be using the supervisord and setting the parameter numprocs of the process to the number of cores you have.

    An example using Circus:

    from circus import get_arbiter
    
    arbiter = get_arbiter("myprogram", numprocesses=3)
    try:
        arbiter.start()
    finally:
        arbiter.stop()
    

提交回复
热议问题