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)

day35-python之协程的更多相关文章

  1. python gevent 协程

    简介 没有切换开销.因为子程序切换不是线程切换,而是由程序自身控制,没有线程切换的开销,因此执行效率高, 不需要锁机制.因为只有一个线程,也不存在同时写变量冲突,在协程中控制共享资源不加锁,只需要判断 ...

  2. 深入理解Python中协程的应用机制: 使用纯Python来实现一个操作系统吧!!

    本文参考:http://www.dabeaz.com/coroutines/   作者:David Beazley 缘起: 本人最近在学习python的协程.偶然发现了David Beazley的co ...

  3. 关于Python的协程问题总结

    协程其实就是可以由程序自主控制的线程 在python里主要由yield 和yield from 控制,可以通过生成者消费者例子来理解协程 利用yield from 向生成器(协程)传送数据# 传统的生 ...

  4. {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二

    python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...

  5. 【Python】协程

    协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在 ...

  6. Python之协程(coroutine)

    Python之协程(coroutine) 标签(空格分隔): Python进阶 coroutine和generator的区别 generator是数据的产生者.即它pull data 通过 itera ...

  7. python的协程和_IO操作

    协程Coroutine: 协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行. 注意,在一个子程序中中断,去执行其他子程序,不是函数调用,有点 ...

  8. python 3 协程函数

    python 3 协程函数 1:把函数的执行结果封装好__iter__和__next__,即得到一个迭代器 2:与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yiel ...

  9. Python之协程函数

    Python之协程函数 什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数. def eater(name): print('%s star ...

  10. 多任务-python实现-协程(2.1.11)

    多任务-python实现-协程(2.1.11) 23/100 发布文章 qq_26624329 @ 目录 1.概念 2.迭代器 1.概念 协程与子例程一样,协程(coroutine)也是一种程序组件. ...

随机推荐

  1. 以SQL命令方式调用存储过程

    string str = "Data Source=.;Initial Catalog=***;Integrated Security=True"; using (SqlConne ...

  2. deepnude | 福利

    程序好下载github有,但是没有lib,就是没有训练好的model. 以下是搜到的win平台程序的下载链接: magnet:?xt=urn:btih:7BE4EB8D640742D2FFEBD649 ...

  3. 支付宝即时到账交易接口C#接入方式的几个坑

    1.在官方文档中 https://docs.open.alipay.com/62/104743 可以清楚看到input_charset前面没有要求加下横杠,可是请求示例是带着的.经过实验得知,这个必须 ...

  4. 一个非常好的开源项目FFmpeg命令处理器FFCH4J

    项目地址:https://github.com/eguid/FFCH4J FFCH4J(原用名:FFmpegCommandHandler4java) FFCH4J项目全称:FFmpeg命令处理器,鉴于 ...

  5. python开发--列表当全局变量来使用

    python中,申明全局变量的时候,一般该变量类型基本上是:字符串或数字: 比较少用“列表”当做变量, 当有作用域限制的情况下,想要外部调用内部作用域的“列表”变量时,可以用这种方式,外部申明一个空列 ...

  6. Java程序执行cmd命令

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  7. 【Git】The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.

    背景,在服务器用www用户身份 执行拉取命令报错 sudo -u www git pull 原因分析: 在新生成密钥之后,在.ssh文件夹中少了known_hosts文件 解决办法: Are you ...

  8. [LeetCode] 507. Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  9. Harbor密码重置 密码修改 admin密码重置

    Harbor密码重置harbor现在是使用postgresql 数据库了.不再支持mysql,网上有N多重置Mysql密码的,可以略过了.我密码错了默认的Harbor12345 修改为: RedHat ...

  10. java的特性与优势

    java的特性与优势 简单性 面向对象 可移植性 高性能 分布式 动态性 多线程 安全性 健壮性