0
A
回答
2
我不知道Python,但我覺得你這樣做,就像你做它在C,通過檢查的fork()
返回值:如果你想在孩子執行一些代碼
child_pid = os.fork()
if child_pid == 0:
print "This is the child."
sys.exit(0)
print "This is the parent."
3
流程,使用multiprocessing
模塊。下面是一個例子,從文檔:
from multiprocessing import Process
def f(name):
print 'hello', name
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
這個例子說明如何使用函數˚F可以在子進程中執行。
相關問題
- 1. 什麼是C?:operator的Ruby等價物?
- 2. Go defer的ruby等價物是什麼?
- 3. 什麼是PHP $ _ENV的Ruby等價物?
- 4. 什麼是Ruby的substr等價物?
- 5. 什麼是preg_quote()的Ruby等價物?
- 6. 什麼是Ruby中的「sys.stdout.write()」等價物?
- 7. 什麼是Python的os.walk的等價物?
- 8. 什麼是Python os.pathsep的等價物?
- 9. 什麼是Tomcat的Python等價物?
- 10. python中'gem'的等價物是什麼?
- 11. 什麼是java.util.zip.Inflater的Python等價物?
- 12. Python中imadjust的等價物是什麼?
- 13. python中print_r()的等價物是什麼?
- 14. Ruby中Python的itertools.product的等價物是什麼?
- 15. 什麼是Python的urllib.quote_plus的Ruby/Rails等價物?
- 16. 什麼是Python的輸出[: - 1]的Ruby等價物?
- 17. 什麼是Ruby的「檢查」的Python等價物?
- 18. 什麼是python setup.py開發的Ruby等價物?
- 19. Ruby中Python文檔字符串的等價物是什麼?
- 20. 是否有「python -i」的ruby等價物?
- 21. Ruby的等價於Python的多處理模塊是什麼?
- 22. Python的延續與Ruby的等價物
- 23. Python的等價物Ruby的each_slice(count)
- 24. Python中的「require」(Ruby)等價於什麼?
- 25. 什麼是PHP的preg_quote的等價物?
- 26. MessageFormat的Pattern.quote()的等價物是什麼?
- 27. 什麼是Ruby的Dir.glob()最簡潔的Clojure等價物?
- 28. 什麼是Java的StringReader的Ruby等價物?
- 29. 什麼是Ruby的「檢查」的JavaScript等價物?
- 30. Haskell中Ruby的pnormaldist統計函數的等價物是什麼?
通過這種方式,子進程也會打印出「This is the parent。」。我如何確保「fork block」之外的任何內容不會在子進程*中執行。 – satoru 2012-08-12 11:18:46
不,這是父母打印「這是父母」。子進入該行之前調用'sys.exit(0)'。它是通過退出塊的最後一個孩子,你確保孩子除了塊之外什麼都不執行。 – 2012-08-12 11:24:39