在子进程或类似于使用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第三方库


热门话题
JavaSwing:实现TableModel还是扩展AbstractTableModel?   无法在连接了SQL的Java中获取数据抛出结果集   java在运行jar时访问jar外部的prop文件   java如何在使用xPath时选择通过检查的节点,而不是它们的父节点。评估()   java如何将文本文件中的整数值翻两番?   java Update Hibernate给出了一个错误   如何使用Java在类中实现调用mule中onCall方法的Singleton?   java如何修复Hibernate 5的映射未找到异常?   调用AlertDialog。按下后退按钮时Android片段的Java生成器   java基于进度更改JProgressBar中的ColorUIResource   java如何让这个测试通过?   java是否可以在TestNG中对参数化测试进行依赖?   java查询无法通过HQL执行   Spring java 11获取spel问题EL1005E:找不到类型   尝试获取JMH锁时发生java异常