微信小程序群发功能代码实现详细分析
939
2022-10-22
pulsar:事件驱动的并发框架
An example of a web server written with pulsar which responds with "Hello World!" for every request:
from pulsar.apps import wsgidef hello(environ, start_response): data = b'Hello World!\n' response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] start_response('200 OK', response_headers) return [data]if __name__ == '__main__': wsgi.WSGIServer(callable=hello).start()
Pulsar's goal is to provide an easy way to build scalable network programs. In the Hello world! web server example above, many client connections can be handled concurrently. Pulsar tells the operating system (through epoll or select) that it should be notified when a new connection is made, and then it goes to sleep.
Pulsar uses the asyncio module from the standard python library and it can be configured to run in multi-processing mode.
Another example of pulsar framework is the asynchronous HttpClient:
from pulsar.apps import httpasync with http.HttpClient() as session: response1 = await session.get('https://github.com/timeline.json') response2 = await session.get('https://api.github.com/emojis.json')
The http client maintains connections alive (by default 15 seconds) and therefore any requests that you make within a session will automatically reuse the appropriate connection. All connections are released once the session exits the asynchronous with block.
Installing
Pulsar has one hard dependency:
multidict
install via pip:
pip install pulsar
or download the tarball from pypi.
To speedup pulsar by a factor of 2 or more these soft dependencies are recommended
httptoolsuvloop
Applications
Pulsar design allows for a host of different asynchronous applications to be implemented in an elegant and efficient way. Out of the box it is shipped with the the following:
Socket serversAsynchronous WSGI serverHttpClientJSON-RPCWeb SocketsAsynchronous Test suiteData stores (with async Redis client)Task queue consumersAsynchronous botocoredjango integration
Examples
Check out the examples directory for various working applications. It includes:
Hello world! wsgi exampleAn Httpbin WSGI applicationAn HTTP Proxy serverA JSON-RPC Calculator serverWebsocket random graph.Websocket chat room.The dining philosophers problem.Twitter streaming
Design
Pulsar internals are based on actors primitive. Actors are the atoms of pulsar's concurrent computation, they do not share state between them, communication is achieved via asynchronous inter-process message passing, implemented using the standard python socket library.
Two special classes of actors are the Arbiter, used as a singleton, and the Monitor, a manager of several actors performing similar functions. The Arbiter runs the main eventloop and it controls the life of all actors. Monitors manage group of actors performing similar functions, You can think of them as a pool of actors.
More information about design and philosophy in the documentation.
Add-ons
Pulsar checks if some additional libraries are available at runtime, and uses them to add additional functionalities or improve performance:
greenlet: required by the pulsar.apps.greenio module and useful for developing implicit asynchronous applicationsuvloop: if available it is possible to use it as the default event loop for actors by passing --io uv in the command line (or event_loop="uv" in the config file)httptools: if available, the default Http Parser for both client and server is replaced by the C implementation in this packagesetproctitle: if installed, pulsar can use it to change the processes names of the running applicationpsutil: if installed, a system key is available in the dictionary returned by Actor info methodpython-certifi: The HttpClient will attempt to use certificates from certifi if it is present on the systemujson: if installed it is used instead of the native json moduleunidecode: to enhance the slugify function
Running Tests
Pulsar test suite uses the pulsar test application. To run tests:
python setup.py test
For options and help type:
python setup.py test --help
flake8 check (requires flake8 package):
flake8
Contributing
Development of pulsar happens at Github. We very much welcome your contribution of course. To do so, simply follow these guidelines:
Fork pulsar on githubCreate a topic branch git checkout -b my_branchPush to your branch git push origin my_branchCreate an issue at https://github.com/quantmind/pulsar/issues with pull request for the dev branch.Alternatively, if you need to report a bug or an unexpected behaviour, make sure to include a mcve in your issue.
A good pull request should:
Cover one bug fix or new feature onlyInclude tests to cover the new code (inside the tests directory)Preferably have one commit only (you can use rebase to combine several commits into one)Make sure flake8 tests pass
License
This software is licensed under the BSD 3-clause License. See the LICENSE file in the top distribution directory for the full license text.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。