Package: aplex
AplexTranslation:简体中文|繁體中文Aplex is a Python library for combining asyncio with
multiprocessing and threading.Aplex helps you run coroutines and functions in other process
or thread with asyncio.Aplex provides a usage like that of  standard libraryconcurrent.futures,
which is familiar to you and intuitive.Aplex lets you do load balancing in a simple way if you need.InstallationFor general users, use the package managerpipto
install aplex.pipinstallaplexFor contributors, install with pipenv:gitclonehttps://github.com/lunluen/aplex.gitcdaplex
pipenvinstall--devor with setuptoolsgitclonehttps://github.com/lunluen/aplex.gitcdaplex
pythonsetup.pydevelopUsageDefinition to know:Aworkis acallableyou want to run with asyncio and multiprocessing or threading.
It can be a coroutine function or just a function.In below case, theworkis the coroutine functiondemo.SubmitYou can submit your work like: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())Note: If you are running python on windows,if __name__ == '__main__':is necessary. That's the design of multiprocessing.Result:Status:200MapFor multiple works, trymap:iterable=('http://httpbin.org'for__inrange(10))forstatusinpool.map(demo,iterable,timeout=10):print('Status:%d.'%status)Awaiting resultsAplex allows one to await results with loop that already exists. It's quite simple.Just set keyword argumentawaitabletoTrue!For example:pool=ProcessAsyncPoolExecutor(awaitable=True)Thenfuture=pool.submit(demo,'http://httpbin.org')status=awaitfutureHow about map?asyncforstatusinpool.map(demo,iterable,timeout=10):print('Status:%d.'%status)Load balancingIn aplex, each worker is the process or thread on your computer. That is, they have the same capability computing.But, your works might have different workloads. Then you need a load balancer.Aplex provides some useful load balancers. They areRoundRobin,Random, andAverage. The default isRoundRobin.Simply set this in contruction keyword argument:fromaplex.load_balancersimportAverageif__name__=='__main__':pool=ProcessAsyncPoolExecutor(load_balancer=Average)Done. So easy. :100:You can also customize one: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_guySee details of how to implement a load balancer at:Worker loop factoryBy the way, if you think the build-in asyncio loop is too slow:importuvloopif__name__=='__main__':pool=ProcessAsyncPoolExecutor(worker_loop_factory=uvloop.Loop)Like this?Scroll up and clickWatch - Releases onlyandStaras a thumbs up! :+1:Any feedback?Feel free to open a issue (just don't abuse it).Or contact me:mas581301@gmail.com:mailbox:Anything about aplex is welcome, such like bugs, system design, variable naming, even English grammer of docstrings!How to contributeContribution are welcome.Asking and advising are also kinds of contribution.Please seeCONTRIBUTING.mdLicenseMIT