在子进程或类似于使用concurrent.futures的线程中运行协同程序和函数。

aplex的Python项目详细描述


Aplex

buildcoverageplatformsupported pythonspackage versionlicensemaintenance

翻译: 简体中文 | 繁體中文

aplex是一个python库,用于将asyncio与 多处理和线程。

  • Aplex帮助您在其他进程中运行协同程序和函数 或异步线程。
  • aplex提供了一个类似于标准库concurrent.futures的用法, 这对你来说很熟悉,也很直观。
  • 如果需要,aplex允许您以简单的方式进行负载平衡。

安装

对于一般用户,使用包管理器pip来 安装Aplex。

pip install aplex

对于贡献者,请使用pipenv安装:

git clone https://github.com/lunluen/aplex.git
cd aplex
pipenv install --dev

或使用设置工具

git clone https://github.com/lunluen/aplex.git
cd aplex
python setup.py develop

用法

要知道的定义:

A work is a callable you want to run with asyncio and multiprocessing or threading. It can be a coroutine function or just a function.

在下面的例子中,work是协程函数demo

提交

您可以提交您的工作,例如:

importaiohttpfromapleximportProcessAsyncPoolExecutorasyncdefdemo(url):asyncwithaiohttp.request('GET',url)asresponse:returnresponse.statusif__name__=='__main__':pool=ProcessAsyncPoolExecutor(pool_size=8)future=pool.submit(demo,'http://httpbin.org')print('Status: %d.'%future.result())

注意:如果您在windows上运行python,if __name__ == '__main__': 是必要的。这就是多处理的设计。

结果:

Status: 200

地图

对于多个作品,请尝试map

iterable=('http://httpbin.org'for__inrange(10))forstatusinpool.map(demo,iterable,timeout=10):print('Status: %d.'%status)

等待结果

允许使用已存在的循环等待结果。很简单。

只需将关键字参数awaitable设置为True

例如:

pool=ProcessAsyncPoolExecutor(awaitable=True)

然后

future=pool.submit(demo,'http://httpbin.org')status=awaitfuture

地图怎么样?

asyncforstatusinpool.map(demo,iterable,timeout=10):print('Status: %d.'%status)

负载平衡

在aplex中,每个worker都是计算机上的进程或线程。也就是说,它们具有相同的计算能力。 但是,您的作品可能有不同的工作负载。那么你需要一个负载均衡器。

aplex提供了一些有用的负载平衡器。它们是RoundRobinRandomAverage。默认值是RoundRobin

只需在construction关键字参数中设置此参数:

fromaplex.load_balancersimportAverageif__name__=='__main__':pool=ProcessAsyncPoolExecutor(load_balancer=Average)

完成。很容易。:100:

您还可以自定义一个:

fromapleximportLoadBalancerclassMyAwesomeLoadBalancer(LoadBalancer):def__init__(*args,**kwargs):super().__init__(*args,**kwargs)# Don't forget this.awesome_attribute='Hello Aplex!'defget_proper_worker(self):the_poor_guy=self.workers[0]returnthe_poor_guy

有关如何实现负载平衡器的详细信息,请参见:

Worker loop factory

By the way, if you think the build-in asyncio loop is too slow:

^{pr 12}$

Like this?

Scroll up and click ^{} and ^{} as a thumbs up! :+1:

Any feedback?

Feel free to open a issue (just don't abuse it).

Or contact me: ^{} :mailbox:

Anything about aplex is welcome, such like bugs, system design, variable naming, even English grammer of docstrings!

How to contribute

Contribution are welcome.

Asking and advising are also kinds of contribution.

Please see CONTRIBUTING.md

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
ArrayList Java中的搜索字符串   另一个web应用程序的java访问会话   另一个应用程序中的活动和服务之间的java通信   java根据Json字符串类型将Json字符串转换为对象   eclipse如何解决java中的错误异常。lang.NoSuchMethodError:'java。字符串javax。摆动JOptionPane。showInputDialog(java.lang.String)'   线程“main”java中的安卓异常。lang.NoClassDefFoundError:org/codehaus/jackson/JsonParseException   java如何在安卓 emulator上显示Mat图像?使用NDK   Java在本地读取测试源文件,但在服务器上读取失败   java dowhile循环用于计算输入数字中的数字。故障排除代码   JAva初学者在编写获取成本的方法时遇到困难   java是shell游戏。我如何让物体移动,特别是在特定的曲线上,但顺序是随机的?   java如何区分两个同名的JButton   java为什么我在Spring Boot中需要一个接口?   java将文件路径插入数据库将删除\   使用InterfaceType初始化java对象   java如何部署一个分为Angular、Spring Boot和MySQL的项目?   java如何使用Symja解决不等式?