day35-python之协程
程序员文章站
2024-01-07 12:42:40
1.协程 2.进程同步 3.进程池 4.进程通信 ......
1.协程
# import time # import queue # # def consumer(name): # # print("--->ready to eat baozi...") # while true: # new_baozi = yield # print("[%s] is eating baozi %s" % (name,new_baozi)) # #time.sleep(1) # def producer(): # # r = con.__next__() # # r = con2.__next__() # # # # n = 0 # # while 1: # # time.sleep(1) # # print("\033[32;1m[producer]\033[0m is making baozi %s and %s" %(n,n+1) ) # # con.send(n) # # con2.send(n+1) # # n +=2 # # # # # # if __name__ == '__main__': # # # # con = consumer("c1") # # con2 = consumer("c2") # # producer() # from greenlet import greenlet # # def test1(): # print(12) # gr2.switch() # print(34) # def test2(): # print(56) # gr1.switch() # print(78) # gr1.switch() # # gr1 = greenlet(test1) # gr2 = greenlet(test2) # gr2.switch() # import gevent # import requests,time # start=time.time() # def f(url): # print('get: %s' % url) # resp =requests.get(url) # data = resp.text # print('%d bytes received from %s.' % (len(data), url)) # # f('https://www.python.org/') # f('https://www.yahoo.com/') # f('https://www.baidu.com/') # f('https://www.sina.com.cn/') # f("http://www.xiaohuar.com/hua/") # # # gevent.joinall([ # # gevent.spawn(f, 'https://www.python.org/'), # # gevent.spawn(f, 'https://www.yahoo.com/'), # # gevent.spawn(f, 'https://www.baidu.com/'), # # gevent.spawn(f, 'https://www.sina.com.cn/'), # # gevent.spawn(f, 'http://www.xiaohuar.com/hua/'), # # ]) # # # f('https://www.python.org/') # # # # f('https://www.yahoo.com/') # # # # f('https://baidu.com/') # # # f('https://www.sina.com.cn/') # # print("cost time:",time.time()-start)
2.进程同步
# from multiprocessing import process, lock # import time # # def f(l, i): # # l.acquire() # time.sleep(1) # print('hello world %s' % i) # l.release() # # if __name__ == '__main__': # lock = lock() # # for num in range(10): # process(target=f, args=(lock, num)).start()
3.进程池
# from multiprocessing import process,pool # import time,os # # def foo(i): # # time.sleep(1) # print(i) # print("son",os.getpid()) # # return "hello %s"%i # from multiprocessing import process,pool # def bar(arg): # print(arg) # # print("hello") # # print("bar:",os.getpid()) # # if __name__ == '__main__': # # pool = pool(5) # print("main pid",os.getpid()) # for i in range(100): # #pool.apply(func=foo, args=(i,)) #同步接口 # #pool.apply_async(func=foo, args=(i,)) # # #回调函数: 就是某个动作或者函数执行成功后再去执行的函数 # # pool.apply_async(func=foo, args=(i,),callback=bar) # # pool.close() # pool.join() # join与close调用顺序是固定的 # # print('end')
4.进程通信
# # # # import queue,time # # import multiprocessing # def foo(q): # time.sleep(1) # print("son process",id(q)) # q.put(123) # q.put("yuan") # # if __name__ == '__main__': # #q=queue.queue() # q=multiprocessing.queue() # p=multiprocessing.process(target=foo,args=(q,)) # p.start() # #p.join() # print("main process",id(q)) # print(q.get()) # print(q.get()) # # # # # # # from multiprocessing import process, pipe # def f(conn): # conn.send([12, {"name":"yuan"}, 'hello']) # response=conn.recv() # print("response",response) # conn.close() # print("q_id2:",id(conn)) # # if __name__ == '__main__': # # parent_conn, child_conn = pipe() #双向管道 # # print("q_id1:",id(child_conn)) # p = process(target=f, args=(child_conn,)) # p.start() # # print(parent_conn.recv()) # prints "[42, none, 'hello']" # parent_conn.send("儿子你好!") # p.join() # # # from multiprocessing import process, manager # # def f(d, l,n): # # d[n] = '1' #{0:"1"} # d['2'] = 2 #{0:"1","2":2} # # l.append(n) #[0,1,2,3,4, 0,1,2,3,4,5,6,7,8,9] # #print(l) # # if __name__ == '__main__': # # with manager() as manager: # # d = manager.dict()#{} # # l = manager.list(range(5))#[0,1,2,3,4] # # # p_list = [] # # for i in range(10): # p = process(target=f, args=(d,l,i)) # p.start() # p_list.append(p) # # for res in p_list: # res.join() # # print(d) # print(l)