python和c混合编程 gil,如何在python中使用C扩展来解决GIL
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 on this?
解決方案
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.
總結
以上是生活随笔為你收集整理的python和c混合编程 gil,如何在python中使用C扩展来解决GIL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 门诊记录保险查的到吗
- 下一篇: TensorFlow实验(2)