import os, timeif __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, sysif __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)# parentos.wait()
from multiprocessing import Process import timen = 100def pro_test(arg):print('running argument is %s' % arg)global nn += 1print('n的地址:{}, n:{}'.format(id(n), n))time.sleep(1)def pro2_test():global nprint('n的地址:{}, n:{}'.format(id(n), n))print('n:%d' % n)print('''process 2......''')# 进程的构建方式二 class Myprocess(Process):def __init__(self, args):super().__init__()self.args = argsdef 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_testpro2.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 timedef 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 timedef job():while True:print('hello')time.sleep(1)if __name__ == '__main__':pro = Process(target=job)pro.start()
from threading import Thread, Lock import timelock = Lock() # 保护互斥量 n = 100 # 多线程发成竞争的那段代码就是互斥量def thr_job():print('the new thread is running....')global nlock.acquire()n = 200lock.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 = sif __name__ == '__main__':lc.name = 'main'thr = Thread(target=fun, args=('python',))thr.start()thr.join()print(lc.name)