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. Eclipse导入工程提示“No projects are found to import”

    如果发现导入工程的时候,出现"No projects are found to import" 的提示,首先查看项目目录中是否有隐藏文件.project,还有目录结构也还要有一个隐 ...

  2. Intellij idea 告警:'while' statement cannot complete without throwing an exception

    有时候这个告警是多余的,例如我们手写的监控线程. 如果有消除告警强迫症.在线程的执行方法上加入注解. @SuppressWarnings("InfiniteLoopStatement&quo ...

  3. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...

  4. sass、less和stylus 相同与不同

    sass.less和stylus的安装使用和入门实践 https://www.jianshu.com/p/1eaf366814e2 stylus 基础教程 https://blog.csdn.net/ ...

  5. egg.js 相关

      egg sequelize 建表规范 CREATE TABLE `wx_member` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'primary key' ...

  6. ROS tf监听编写

    博客转载自:https://www.ncnynl.com/archives/201702/1311.html ROS与C++入门教程-tf-编写tf listener(监听) 说明: 介绍如何使用tf ...

  7. Robotics Education and Research at Scale - A Remotely Accessible Robotics Development Platform

    张宁  Robotics Education and Research at Scale - A Remotely Accessible Robotics Development Platform链接 ...

  8. 开发人员不得不知的MySQL索引和查询优化

    转载:https://blog.csdn.net/enmotech/article/details/88809822 本文主要总结了慢查询优化的过程中常用的以及不合理的操作,适合有 MySQL 基础的 ...

  9. Spring cloud微服务安全实战-7-5配置grafana图表及报警

    先过一下grafana的配置文件 grafana的配置文件. 右键服务的地址.发信人 账号 和面等 配置要连到prometheus上. 登陆的密码是多少,第二行是不允许用户注册. dashboard. ...

  10. LD SCore计算基因多效性、遗传度、遗传相关性(the LD Score regression intercept, heritability and genetic correlation)

    这篇文章是对之前啊啊救救我,为何我的QQ图那么飘(全基因组关联分析)这篇文章的一个补坑. LD SCore除了查看显著SNP位点对表型是否为基因多效性外,还额外补充了怎么计算表型的遗传度和遗传相关性. ...