day35-python之协程

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)

猜你喜欢

转载自www.cnblogs.com/sqy-yyr/p/11439817.html
今日推荐