原创博客地址:python进阶21之actor

actor模型。actor模式是一种最古老的也是最简单的并行和分布式计算解决方案。
优点:充分利用单线程+事件机制,达到了多线程效果。
缺点,对python而言,由于GIL的存在,毕竟只是单线程,难以匹敌多进程,目前使用并不多。

简单任务调度器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class TaskScheduler:def __init__(self):self._task_queue = deque()def new_task(self, task):'''Admit a newly started task to the scheduler'''self._task_queue.append(task)def run(self):'''Run until there are no more tasks'''while self._task_queue:task = self._task_queue.popleft()try:# Run until the next yield statementnext(task)self._task_queue.append(task)except StopIteration:# Generator is no longer executingpass# Example use
sched = TaskScheduler()
sched.new_task(countdown(10))
sched.new_task(countdown(5))
sched.new_task(countup(15))
sched.run()

协程生产者消费者

廖雪峰的python官网教程里面的协程生产者消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def consumer():r = ''while True:n = yield rif not n:returnprint('[CONSUMER] Consuming %s...' % n)r = '200 OK'def produce(c):c.send(None)n = 0while n < 5:n = n + 1print('[PRODUCER] Producing %s...' % n)r = c.send(n)print('[PRODUCER] Consumer return: %s' % r)c.close()c = consumer()
produce(c)

并发网络应用程序

演示了使用生成器来实现一个并发网络应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class ActorScheduler:def __init__(self):self._actors = {}self._msg_queue = deque()def new_actor(self, name, actor):self._msg_queue.append((actor, None))self._actors[name] = actordef send(self, name, msg):actor = self._actors.get(name)if actor:self._msg_queue.append((actor, msg))def run(self):while self._msg_queue:# print("队列:", self._msg_queue)actor, msg = self._msg_queue.popleft()# print("actor", actor)# print("msg", msg)try:actor.send(msg)except StopIteration:passif __name__ == '__main__':def say_hello():while True:msg = yieldprint("say hello", msg)def say_hi():while True:msg = yieldprint("say hi", msg)def counter(sched):while True:n = yieldprint("counter:", n)if n == 0:breaksched.send('say_hello', n)sched.send('say_hi', n)sched.send('counter', n-1)sched = ActorScheduler()# 创建初始化 actorssched.new_actor('say_hello', say_hello())sched.new_actor('say_hi', say_hi())sched.new_actor('counter', counter(sched))sched.send('counter', 10)sched.run()

参考

扩展Python Gevent的Actor模型:https://www.dazhuanlan.com/2020/02/29/5e5a7f241ed15/
终结python协程—-从yield到actor模型的实现:https://www.bbsmax.com/A/n2d9bQaYzD/
12.12 使用生成器代替线程:https://python3-cookbook.readthedocs.io/zh_CN/latest/c12/p12_using_generators_as_alternative_to_threads.html

python进阶21之actor相关推荐

  1. Python进阶之递归函数的用法及其示例

    作者 | 程序员adny 责编 | 徐威龙 封图| CSDN│下载于视觉中国 出品 |  AI科技大本营(ID:rgznai100) 本篇文章主要介绍了Python进阶之递归函数的用法及其示例,现在分 ...

  2. 如果只推荐一本 Python 进阶的书,我要 Pick 它!

    作者 | 豌豆花下猫 今年二月初,我偶然看到了一条推特: <流畅的Python>一书的作者发布了一条激动人心的消息:他正在写作第二版! 如果要票选最佳的 Python 进阶类书目,这本书肯 ...

  3. python进阶书籍推荐-豆瓣评分9.4!年度最值得推荐的Python进阶书

    原标题:豆瓣评分9.4!年度最值得推荐的Python进阶书 来自:程序员书库(ID:OpenSourceTop) 编译 链接:https://whatpixel.com/fluent-python-b ...

  4. python进阶 多线程编程 —— threading和queue库实现多线程编程

    python进阶 多线程编程 -- threading和queue库实现多线程编程) 摘要 多线程实现逻辑封装 模型参数选择实例 摘要 本文主要介绍了利用python的 threading和queue ...

  5. Python进阶(2)

    Python进阶(2) 题目来自于慕课网,python进阶课程 本系列笔记来自于慕课网的学习成果. 题目1 Python中自定义排序函数 Python内置的sorted()函数可对list进行排序: ...

  6. python进阶20装饰器

    原创博客地址:python进阶20装饰器 Nested functions Python允许创建嵌套函数,这意味着我们可以在函数内声明函数并且所有的作用域和声明周期规则也同样适用. 1 2 3 4 5 ...

  7. python进阶19垃圾回收GC

    原创博客链接:python进阶19垃圾回收GC 垃圾收集三大手段 一.引用计数(计数器) Python垃圾回收主要以引用计数为主,分代回收为辅.引用计数法的原理是每个对象维护一个ob_ref,用来记录 ...

  8. python进阶17炫技巧

    原创博客链接:python进阶17炫技巧 原则:可读性第一(效率固然重要,除非非常明显的效率差异,否则可读性优先) 学习炫技巧,更多为了读懂他人代码,自己开发过程中,相似代码量(可读性),建议使用通俗 ...

  9. python进阶16多继承与Mixin

    原创博客链接: python进阶16多继承与Mixin Mixin解释 为了让大家,对这个 Mixin 有一个更直观的理解,摘录了网上一段说明. 民航飞机是一种交通工具,对于土豪们来说直升机也是一种交 ...

最新文章

  1. 问题:为命名空间,在此被用作类型和此项目作为引用添加将导致循环依赖项
  2. 【系统分析与设计】UML协作图绘制方法(真の能看懂~!)
  3. Django中的日期和时间格式 DateTimeField
  4. Python学习:基本概念
  5. 【数字信号处理】希尔伯特变换系列1之相位处理(含MATLAB代码)
  6. 【ECharts系列|01入门】 从入门到天黑【入门级教程实战】
  7. 前端学习(1272):Vue前端路由
  8. 拍不完的脑袋:推荐系统打压保送重排策略
  9. HTML+CSS+JS实现 ❤️等离子球体ui动画特效❤️
  10. (计算机组成原理)第五章中央处理器-第三节1:CPU内部单总线数据通路中数据的流动
  11. Spring、SpringMVC、Spring Boot、Spring Cloud 概念、关系及区别
  12. Spring框架----Spring框架的基本概念
  13. MS SQL入门基础:管理触发器
  14. 【VS2010学习笔记】【编程实例】 (在Visual Studio中使用C++创建和使用DLL)
  15. Nginx为什么会比Apache Httpd高效
  16. 【bug解决】No OpKernel was registered to support Op 'CudnnRNN' with these attrs.
  17. K8S-删除Terminating状态的namespace
  18. 1.2 XML 的基本语法
  19. c语言中立方和乘法怎么表示什么区别,C语言程式设计中的平方立方怎么表示
  20. oracle数据库imp命令,数据库imp导入命令

热门文章

  1. C++继承时的一些细节知识点
  2. 索引sql server_优化SQL Server索引策略
  3. azure blob_在Azure中使用表,blob,队列和文件存储
  4. 如何使用SQL Server数据工具中的“可见性”选项降低报告的复杂性
  5. SQL Server Always On可用性组中的Windows故障转移群集仲裁模式
  6. 处理页面动态加载数据
  7. shiro 实现自己定义权限规则校验
  8. freemarker框架 在easyui页面中处理数字 比如在页面得到1,234
  9. XSS、CSRF与验证码等等
  10. WPF中的图像处理简介