...
""" 创建2个进程, 每一个进程都打出当前的时间和进程的标识,持续打十次. """ import multiprocessing as mul from datetime import datetime import time def worker(id): for i in range(10): now = datetime.now() print('proc #%s %s' % (id, now), flush=True) if __name__ == '__main__': procs = [] # 创建进程,并把进程对象保存起来 for i in range(2): p = mul.Process(target=worker, args=(i,)) p.start() procs.append(p) # 用join方法等待进程结束 for p in procs: p.join() time.sleep(3)