直接创建进程
from multiprocessingimport Process
import os,time
def ceshi(test):
time.sleep(test)
print("我是子进程%d,我的父进程是%d" % (os.getpid(), os.getppid()))
print(test)
if __name__ =="__main__":
print("进程开始")
p = Process(target=ceshi, args=(2,))
p1 = Process(target=ceshi, args=(3,))
p.start()# 开启进程
p1.start()# 开启进程
p.join()
p1.join()
print("进程结束")
这里的join意思是,主进程需要等待这个进程执行完毕再结束主进程。
进程池:
from multiprocessingimport Pool
#进程池的作用:把进程放到池子里,一次并行有限个(比如4个),一旦执行完,然后下一组放入进程池,直到进程池里面没有进程。可以有效解决,并发带来的内存崩溃等问题,而且有效利用了多核,相比同步执行,起码快4倍。
import os,time
def ceshi(test):
time.sleep(2)
print(test,os.getpid(),os.getppid())
if __name__=="__main__":
test="Hello World"
pool=Pool()
for iin range(10):
pool.apply_async(ceshi,args=(test,))
pool.close()#这里意思是,关闭进程池,不让插入了,下面是让主进程等待子进程运行结束,再关闭主进程
pool.join()
#进程的通信
进程通信的方式:共享内存,管道,消息传送
from multiprocessingimport Queue,Process
import os,time,random
def shuru(q):
for iin ["a","b","c"]:
q.put(i)
time.sleep(random.random())
def shuchu(q):
while True:
temp=q.get(True)
print(temp)
if __name__=="__main__":
q=Queue()
p1=Process(target=shuru,args=(q,))
p2=Process(target=shuchu,args=(q,))
p1.start()
p2.start()
p1.join()
p2.terminate()
#线程
from threading import Thread
def ceshi(test):
print(test)
t=Thread(target=ceshi,args=(test,))
t.start()
#线程锁
因为线程是共享全局变量的,存在竞争,所以要对全局变量加锁控制变量污染
from threading import Thread,Lock
lock=Lock()
b=0
def change(n):
global b
b=b+n
b=b-n
def xunhuan(n):
for i in range(100000):
with lock:
change(n)
t1=Thread(target=xunhuan,args=(5,))
t2=Thread(target=xunhuan,args=(8,))
t1.start()
t2.start()
join的作用,同进程
import threading
import time
def tstart(arg):
print("%s running....at: %s" % (arg,time.time()))
time.sleep(1)
print("%s is finished! at: %s" % (arg,time.time()))
if __name__ == '__main__':
t1 = threading.Thread(target=tstart, args=('This is thread 1',))
t1.start()
t1.join() # 当前线程阻塞,等待t1线程执行完成
print("This is main function at:%s" % time.time())
deamon守护线程
可以通过将创建的线程指定为守护线程(daemon),这样主线程执行完毕之后会立即结束未执行完的线程,然后结束程序。
import threading
import time
def tstart(arg):
print("%s running....at: %s" % (arg,time.time()))
time.sleep(1)
print("%s is finished! at: %s" % (arg,time.time()))
if __name__ == '__main__':
t1 = threading.Thread(target=tstart, args=('This is thread 1',))
t1.setDaemon(True)
t1.start()
# t1.join() # 当前线程阻塞,等待t1线程执行完成
print("This is main function at:%s" % time.time())
#12.线程池和进程池,为什么要讲这个呢?另一个高级库实现线程池和进程池
# import time
# from concurrent.futures import Future
# from concurrent.futures.thread import ThreadPoolExecutor
# from concurrent.futures.process import ProcessPoolExecutor
# def func(value):
# time.sleep(1)
# print(value)
# pool = ThreadPoolExecutor(max_workers=5)
# # 或 pool = ProcessPoolExecutor(max_workers=5)
# for i in range(10):
# fut = pool.submit(func, i)
# print(fut)
# print(type(fut))#<class 'concurrent.futures._base.Future'>
网友评论