@Scrazy
2016-02-15T08:37:13.000000Z
字数 519
阅读 960
python学习笔记
Linux和Unix系统提供了fork()
函数,这个函数可以调用一次,返回两次,自动复制一份(子进程)。子进程永远返回0,子进程可以通过getppid()拿到父进程的ID。
Python的OS模块封装了常见的系统调用,Python可以通过fork()
轻松创建子进程。
#/home/mouse python3
#-*- coding: utf-8 -*-
#倒入os模块
import os
#当前进程
print('Process (%s) start...' % os.getpid())
#获得父进程
pid = os.fork()
if pid == 0:
print('I am child process (%s) and my partent is %s.'
% (os.getpid(), os.getppid()))
else:
print("I (%s) just created a child process (%s)."
% (os.getpid(), pid))
没有理解为什么if语句和else语句都被执行了[^footnote] mark
Windows没有fork
调用,故可以使用multiprocessing模块进行跨平台编程