How to use C extensions in python to get around GIL

后端 未结 5 1136
走了就别回头了
走了就别回头了 2021-02-19 23:51

I want to run a cpu intensive program in Python across multiple cores and am trying to figure out how to write C extensions to do this. Are there any code samples or tutorials

5条回答
  •  不思量自难忘°
    2021-02-20 00:35

    You can already break a Python program into multiple processes. The OS will already allocate your processes across all the cores.

    Do this.

    python part1.py | python part2.py | python part3.py | ... etc.
    

    The OS will assure that part uses as many resources as possible. You can trivially pass information along this pipeline by using cPickle on sys.stdin and sys.stdout.

    Without too much work, this can often lead to dramatic speedups.

    Yes -- to the haterz -- it's possible to construct an algorithm so tortured that it may not be sped up much. However, this often yields huge benefits for minimal work.

    And.

    The restructuring for this purpose will exactly match the restructuring required to maximize thread concurrency. So. Start with shared-nothing process parallelism until you can prove that sharing more data would help, then move to the more complex shared-everything thread parallelism.

提交回复
热议问题