import os, time if __name__ == '__main__': print('the calling process id:%d' % os.getpid()) # 创建进程 pid = os.fork() if pid == 0: # 子进程 print('the child pid is %d' % os.getpid()) time.sleep(3) elif pid > 0: # 父进程 os.wait() # 等待子进程终止 print('[%d]bye-bye' % os.getpid())
import os, sys if __name__ == '__main__': while True: cmd = input('[root@localhost xxxx]# ') if cmd == 'quit' or cmd == 'exit': # 终止进程 sys.exit(0) # os._exit() # 'ls -l' cmdls = cmd.split() pid = os.fork() if pid == 0: # child 替换调用进程 os.execlp(cmdls[0], *cmdls) # parent os.wait()
from multiprocessing import Process import time n = 100 def pro_test(arg): print('running argument is %s' % arg) global n n += 1 print('n的地址:{}, n:{}'.format(id(n), n)) time.sleep(1) def pro2_test(): global n print('n的地址:{}, n:{}'.format(id(n), n)) print('n:%d' % n) print('''process 2......''') # 进程的构建方式二 class Myprocess(Process): def __init__(self, args): super().__init__() self.args = args def run(self): print('hello world with %s' % self.args) if __name__ == '__main__': pro = Process(target=pro_test, args=('python',)) # 运行 pro.start() # 收尸 pro.join() print(pro.name, pro.pid) pro2 = Process() pro2.run = pro2_test pro2.start() # 启动进程, 并调用run方法 pro2.join() print(pro2.name, pro2.pid) pro3 = Myprocess('the argument of the process') pro3.start() pro3.join()
两个进程全局变量是独立的:
from multiprocessing import Process, Pipe import time # 发送 def pro1(args): for i in range(10): # 向管道发送数据 args.send(i) time.sleep(1) args.close() def pro2(args): while True: # 从管道接收数据 r = args.recv() print('from pro1:%d' % r) if __name__ == '__main__': # 管道的创建,得到管道的两端 conn1, conn2 = Pipe() obj1 = Process(target=pro1, args=(conn1,)) obj2 = Process(target=pro2, args=(conn2,)) obj1.start() obj2.start() obj1.join() obj2.join()
from multiprocessing import Process, Queue import time # 发送 def pro1(que): for i in range(10): # 向队列中写入 que.put(i) time.sleep(2) def pro2(que): while True: print('from que get:{}'.format(que.get())) if __name__ == '__main__': # 创建队列 q = Queue() obj1 = Process(target=pro1, args=(q,)) obj2 = Process(target=pro2, args=(q,)) obj1.start() obj2.start() obj1.join() obj2.join()
from multiprocessing import Pool import time def pro_job(s): print('the %d process is running' % s) time.sleep(2) if __name__ == '__main__': # 创建进程池 pool = Pool(2) for i in range(5): # 异步添加进程 pool.apply_async(func=pro_job, args=(i, )) # 不允许向进程池中添加新的进程 pool.close() # 收尸 pool.join()
from multiprocessing import Process import time def job(): while True: print('hello') time.sleep(1) if __name__ == '__main__': pro = Process(target=job) pro.start()
from threading import Thread, Lock import time lock = Lock() # 保护互斥量 n = 100 # 多线程发成竞争的那段代码就是互斥量 def thr_job(): print('the new thread is running....') global n lock.acquire() n = 200 lock.release() time.sleep(3) if __name__ == '__main__': # 创建线程 thr = Thread(target=thr_job, name='不一样', daemon=True) thr.start() print(thr.name, thr.ident) # 加锁 lock.acquire() print(n) time.sleep(2) print(n + 10) # 解锁 lock.release() thr.join()
n = 0 :任务没发下来等待继续发放任务
n > 0:还有任务发放
n < 0 :没有任务发放完成了
from threading import Thread, local # 使得多线程在使用全局的local绑定变量的时候局部化 lc = local() def fun(s): lc.name = s if __name__ == '__main__': lc.name = 'main' thr = Thread(target=fun, args=('python',)) thr.start() thr.join() print(lc.name)