How to run two functions simultaneously

后端 未结 4 386
[愿得一人]
[愿得一人] 2020-12-17 15:07

I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check th

4条回答
  •  佛祖请我去吃肉
    2020-12-17 15:45

    If you want to use the common Python implementation (CPython), you can certainly use the multiprocessing module, which does wonders (you can pass non-pickleable arguments to subprocesses, kill tasks,…), offers an interface similar to that of threads, and does not suffer from the Global Interpreter Lock.

    The downside is that subprocesses are spawned, which takes more time than creating threads; this should only be a problem if you have many, many short tasks. Also, since data is passed (via serialization) between processes, large data both takes a long time to pass around and ends up having a large memory footprint (as it is duplicated between each process). In situations where each task takes a "long" time and the data in and out of each task is not too large, the multiprocessing module should be great.

提交回复
热议问题