簡體   English   中英

Python相當於Ruby的fork塊是什么?

[英]What is the Python equivalent of Ruby's fork block?

使用Ruby,可以使用fork塊來表示

此塊中的語句僅在子進程中執行,並且將由父進程跳過。

Python中有類似的東西嗎?

如果要在子進程中執行某些代碼,請使用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

我不了解Python,但是我認為您可以通過檢查fork()的返回值來像在C中一樣進行操作:

child_pid = os.fork()
if child_pid == 0:
    print "This is the child."
    sys.exit(0)
print "This is the parent."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM