Rubyfork
ならブロックで表現できる
このブロック内のステートメントは、子プロセスでのみ実行され、親プロセスによってスキップされます。
Pythonに似たようなものはありますか?
子プロセスでコードを実行したい場合は、multiprocessing
module を使用します。ドキュメントの例を次に示します。
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 を知りませんが、C で行うのと同じように、 の戻り値をチェックすることで行うと思いますfork()
。
child_pid = os.fork()
if child_pid == 0:
print "This is the child."
sys.exit(0)
print "This is the parent."