Pyjo-Reactor-Gevent 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyjo_reactor_gevent-0.1.0/.gitignore +17 -0
- pyjo_reactor_gevent-0.1.0/.proverc +2 -0
- pyjo_reactor_gevent-0.1.0/LICENSE +201 -0
- pyjo_reactor_gevent-0.1.0/PKG-INFO +226 -0
- pyjo_reactor_gevent-0.1.0/Pyjo/Reactor/Gevent.py +565 -0
- pyjo_reactor_gevent-0.1.0/README.md +208 -0
- pyjo_reactor_gevent-0.1.0/docs/conf.py +116 -0
- pyjo_reactor_gevent-0.1.0/docs/index.rst +53 -0
- pyjo_reactor_gevent-0.1.0/examples/greenlets_gevent.py +42 -0
- pyjo_reactor_gevent-0.1.0/examples/hello_world_gevent.py +13 -0
- pyjo_reactor_gevent-0.1.0/examples/loopback_gevent.py +25 -0
- pyjo_reactor_gevent-0.1.0/pyproject.toml +91 -0
- pyjo_reactor_gevent-0.1.0/t/Reactor_Gevent.py +764 -0
- pyjo_reactor_gevent-0.1.0/t/docs/Reactor_Gevent.py +71 -0
- pyjo_reactor_gevent-0.1.0/t/examples.py +86 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
The Artistic License 2.0
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2000-2006, The Perl Foundation.
|
|
4
|
+
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
Preamble
|
|
9
|
+
|
|
10
|
+
This license establishes the terms under which a given free software
|
|
11
|
+
Package may be copied, modified, distributed, and/or redistributed.
|
|
12
|
+
The intent is that the Copyright Holder maintains some artistic
|
|
13
|
+
control over the development of that Package while still keeping the
|
|
14
|
+
Package available as open source and free software.
|
|
15
|
+
|
|
16
|
+
You are always permitted to make arrangements wholly outside of this
|
|
17
|
+
license directly with the Copyright Holder of a given Package. If the
|
|
18
|
+
terms of this license do not permit the full use that you propose to
|
|
19
|
+
make of the Package, you should contact the Copyright Holder and seek
|
|
20
|
+
a different licensing arrangement.
|
|
21
|
+
|
|
22
|
+
Definitions
|
|
23
|
+
|
|
24
|
+
"Copyright Holder" means the individual(s) or organization(s)
|
|
25
|
+
named in the copyright notice for the entire Package.
|
|
26
|
+
|
|
27
|
+
"Contributor" means any party that has contributed code or other
|
|
28
|
+
material to the Package, in accordance with the Copyright Holder's
|
|
29
|
+
procedures.
|
|
30
|
+
|
|
31
|
+
"You" and "your" means any person who would like to copy,
|
|
32
|
+
distribute, or modify the Package.
|
|
33
|
+
|
|
34
|
+
"Package" means the collection of files distributed by the
|
|
35
|
+
Copyright Holder, and derivatives of that collection and/or of
|
|
36
|
+
those files. A given Package may consist of either the Standard
|
|
37
|
+
Version, or a Modified Version.
|
|
38
|
+
|
|
39
|
+
"Distribute" means providing a copy of the Package or making it
|
|
40
|
+
accessible to anyone else, or in the case of a company or
|
|
41
|
+
organization, to others outside of your company or organization.
|
|
42
|
+
|
|
43
|
+
"Distributor Fee" means any fee that you charge for Distributing
|
|
44
|
+
this Package or providing support for this Package to another
|
|
45
|
+
party. It does not mean licensing fees.
|
|
46
|
+
|
|
47
|
+
"Standard Version" refers to the Package if it has not been
|
|
48
|
+
modified, or has been modified only in ways explicitly requested
|
|
49
|
+
by the Copyright Holder.
|
|
50
|
+
|
|
51
|
+
"Modified Version" means the Package, if it has been changed, and
|
|
52
|
+
such changes were not explicitly requested by the Copyright
|
|
53
|
+
Holder.
|
|
54
|
+
|
|
55
|
+
"Original License" means this Artistic License as Distributed with
|
|
56
|
+
the Standard Version of the Package, in its current version or as
|
|
57
|
+
it may be modified by The Perl Foundation in the future.
|
|
58
|
+
|
|
59
|
+
"Source" form means the source code, documentation source, and
|
|
60
|
+
configuration files for the Package.
|
|
61
|
+
|
|
62
|
+
"Compiled" form means the compiled bytecode, object code, binary,
|
|
63
|
+
or any other form resulting from mechanical transformation or
|
|
64
|
+
translation of the Source form.
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
Permission for Use and Modification Without Distribution
|
|
68
|
+
|
|
69
|
+
(1) You are permitted to use the Standard Version and create and use
|
|
70
|
+
Modified Versions for any purpose without restriction, provided that
|
|
71
|
+
you do not Distribute the Modified Version.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Permissions for Redistribution of the Standard Version
|
|
75
|
+
|
|
76
|
+
(2) You may Distribute verbatim copies of the Source form of the
|
|
77
|
+
Standard Version of this Package in any medium without restriction,
|
|
78
|
+
either gratis or for a Distributor Fee, provided that you duplicate
|
|
79
|
+
all of the original copyright notices and associated disclaimers. At
|
|
80
|
+
your discretion, such verbatim copies may or may not include a
|
|
81
|
+
Compiled form of the Package.
|
|
82
|
+
|
|
83
|
+
(3) You may apply any bug fixes, portability changes, and other
|
|
84
|
+
modifications made available from the Copyright Holder. The resulting
|
|
85
|
+
Package will still be considered the Standard Version, and as such
|
|
86
|
+
will be subject to the Original License.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
Distribution of Modified Versions of the Package as Source
|
|
90
|
+
|
|
91
|
+
(4) You may Distribute your Modified Version as Source (either gratis
|
|
92
|
+
or for a Distributor Fee, and with or without a Compiled form of the
|
|
93
|
+
Modified Version) provided that you clearly document how it differs
|
|
94
|
+
from the Standard Version, including, but not limited to, documenting
|
|
95
|
+
any non-standard features, executables, or modules, and provided that
|
|
96
|
+
you do at least ONE of the following:
|
|
97
|
+
|
|
98
|
+
(a) make the Modified Version available to the Copyright Holder
|
|
99
|
+
of the Standard Version, under the Original License, so that the
|
|
100
|
+
Copyright Holder may include your modifications in the Standard
|
|
101
|
+
Version.
|
|
102
|
+
|
|
103
|
+
(b) ensure that installation of your Modified Version does not
|
|
104
|
+
prevent the user installing or running the Standard Version. In
|
|
105
|
+
addition, the Modified Version must bear a name that is different
|
|
106
|
+
from the name of the Standard Version.
|
|
107
|
+
|
|
108
|
+
(c) allow anyone who receives a copy of the Modified Version to
|
|
109
|
+
make the Source form of the Modified Version available to others
|
|
110
|
+
under
|
|
111
|
+
|
|
112
|
+
(i) the Original License or
|
|
113
|
+
|
|
114
|
+
(ii) a license that permits the licensee to freely copy,
|
|
115
|
+
modify and redistribute the Modified Version using the same
|
|
116
|
+
licensing terms that apply to the copy that the licensee
|
|
117
|
+
received, and requires that the Source form of the Modified
|
|
118
|
+
Version, and of any works derived from it, be made freely
|
|
119
|
+
available in that license fees are prohibited but Distributor
|
|
120
|
+
Fees are allowed.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
Distribution of Compiled Forms of the Standard Version
|
|
124
|
+
or Modified Versions without the Source
|
|
125
|
+
|
|
126
|
+
(5) You may Distribute Compiled forms of the Standard Version without
|
|
127
|
+
the Source, provided that you include complete instructions on how to
|
|
128
|
+
get the Source of the Standard Version. Such instructions must be
|
|
129
|
+
valid at the time of your distribution. If these instructions, at any
|
|
130
|
+
time while you are carrying out such distribution, become invalid, you
|
|
131
|
+
must provide new instructions on demand or cease further distribution.
|
|
132
|
+
If you provide valid instructions or cease distribution within thirty
|
|
133
|
+
days after you become aware that the instructions are invalid, then
|
|
134
|
+
you do not forfeit any of your rights under this license.
|
|
135
|
+
|
|
136
|
+
(6) You may Distribute a Modified Version in Compiled form without
|
|
137
|
+
the Source, provided that you comply with Section 4 with respect to
|
|
138
|
+
the Source of the Modified Version.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
Aggregating or Linking the Package
|
|
142
|
+
|
|
143
|
+
(7) You may aggregate the Package (either the Standard Version or
|
|
144
|
+
Modified Version) with other packages and Distribute the resulting
|
|
145
|
+
aggregation provided that you do not charge a licensing fee for the
|
|
146
|
+
Package. Distributor Fees are permitted, and licensing fees for other
|
|
147
|
+
components in the aggregation are permitted. The terms of this license
|
|
148
|
+
apply to the use and Distribution of the Standard or Modified Versions
|
|
149
|
+
as included in the aggregation.
|
|
150
|
+
|
|
151
|
+
(8) You are permitted to link Modified and Standard Versions with
|
|
152
|
+
other works, to embed the Package in a larger work of your own, or to
|
|
153
|
+
build stand-alone binary or bytecode versions of applications that
|
|
154
|
+
include the Package, and Distribute the result without restriction,
|
|
155
|
+
provided the result does not expose a direct interface to the Package.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
Items That are Not Considered Part of a Modified Version
|
|
159
|
+
|
|
160
|
+
(9) Works (including, but not limited to, modules and scripts) that
|
|
161
|
+
merely extend or make use of the Package, do not, by themselves, cause
|
|
162
|
+
the Package to be a Modified Version. In addition, such works are not
|
|
163
|
+
considered parts of the Package itself, and are not subject to the
|
|
164
|
+
terms of this license.
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
General Provisions
|
|
168
|
+
|
|
169
|
+
(10) Any use, modification, and distribution of the Standard or
|
|
170
|
+
Modified Versions is governed by this Artistic License. By using,
|
|
171
|
+
modifying or distributing the Package, you accept this license. Do not
|
|
172
|
+
use, modify, or distribute the Package, if you do not accept this
|
|
173
|
+
license.
|
|
174
|
+
|
|
175
|
+
(11) If your Modified Version has been derived from a Modified
|
|
176
|
+
Version made by someone other than you, you are nevertheless required
|
|
177
|
+
to ensure that your Modified Version complies with the requirements of
|
|
178
|
+
this license.
|
|
179
|
+
|
|
180
|
+
(12) This license does not grant you the right to use any trademark,
|
|
181
|
+
service mark, tradename, or logo of the Copyright Holder.
|
|
182
|
+
|
|
183
|
+
(13) This license includes the non-exclusive, worldwide,
|
|
184
|
+
free-of-charge patent license to make, have made, use, offer to sell,
|
|
185
|
+
sell, import and otherwise transfer the Package with respect to any
|
|
186
|
+
patent claims licensable by the Copyright Holder that are necessarily
|
|
187
|
+
infringed by the Package. If you institute patent litigation
|
|
188
|
+
(including a cross-claim or counterclaim) against any party alleging
|
|
189
|
+
that the Package constitutes direct or contributory patent
|
|
190
|
+
infringement, then this Artistic License to you shall terminate on the
|
|
191
|
+
date that such litigation is filed.
|
|
192
|
+
|
|
193
|
+
(14) Disclaimer of Warranty:
|
|
194
|
+
THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
|
|
195
|
+
IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
|
|
196
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
|
|
197
|
+
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
|
|
198
|
+
LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
|
|
199
|
+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
200
|
+
DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
|
|
201
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: Pyjo-Reactor-Gevent
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Low level event reactor with gevent support for Pyjoyment.
|
|
5
|
+
Project-URL: Homepage, https://github.com/pyjoyment/Pyjo-Reactor-Gevent
|
|
6
|
+
Project-URL: Documentation, https://pyjoyment.readthedocs.io/projects/pyjo-reactor-gevent/en/stable/
|
|
7
|
+
Author-email: Piotr Roszatycki <piotr.roszatycki@gmail.com>
|
|
8
|
+
License-Expression: Artistic-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: gevent,greenlet,pyjoyment,reactor
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Requires-Dist: gevent>=26.9.0
|
|
16
|
+
Requires-Dist: pyjoyment>=0.3.0
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
[](https://github.com/pyjoyment/Pyjoyment)
|
|
20
|
+
[](https://github.com/pyjoyment/Pyjo-Reactor-Gevent)
|
|
21
|
+
[](https://pyjoyment.readthedocs.io/projects/pyjo-reactor-gevent/en/stable/)
|
|
22
|
+
[](https://github.com/pyjoyment/Pyjo-Reactor-Gevent/blob/main/LICENSE)
|
|
23
|
+
[](https://pypi.org/project/Pyjo-Reactor-Gevent/)
|
|
24
|
+
[](https://github.com/pyjoyment/Pyjo-Reactor-Gevent/actions/workflows/tests.yaml)
|
|
25
|
+
[](https://github.com/pyjoyment/Pyjo-Reactor-Gevent/actions/workflows/trunk-check.yaml)
|
|
26
|
+
|
|
27
|
+
# Pyjo-Reactor-Gevent
|
|
28
|
+
|
|
29
|
+
<p align="center">
|
|
30
|
+
<picture>
|
|
31
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/pyjoyment/Pyjoyment/main/Pyjoyment/resources/public/pyjo/logo-white-4x.png">
|
|
32
|
+
<img src="https://raw.githubusercontent.com/pyjoyment/Pyjoyment/main/Pyjoyment/resources/public/pyjo/logo-black-4x.png" alt="Pyjoyment" width="322">
|
|
33
|
+
</picture>
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
Low level event reactor with [gevent](https://www.gevent.org) support for
|
|
37
|
+
[Pyjoyment](https://github.com/pyjoyment/Pyjoyment).
|
|
38
|
+
|
|
39
|
+
`Pyjo.Reactor.Gevent` is a reactor of Pyjoyment on the hub of gevent, whose
|
|
40
|
+
loop of libev or libuv watches the handles and runs the timers, so that the
|
|
41
|
+
servers of Pyjoyment, `daemon`, `prefork` and `prethread`, and its user agent
|
|
42
|
+
run along with the greenlets of a program of gevent.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
From [PyPI](https://pypi.org/project/Pyjo-Reactor-Gevent/), with
|
|
47
|
+
[Pyjoyment](https://pypi.org/project/Pyjoyment/) and
|
|
48
|
+
[gevent](https://pypi.org/project/gevent/), which it depends on:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
pip install Pyjo-Reactor-Gevent
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
or, in a project managed with [uv](https://docs.astral.sh/uv/):
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
uv add Pyjo-Reactor-Gevent
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The development version is installed from the Git repository:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pip install git+https://github.com/pyjoyment/Pyjo-Reactor-Gevent
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
Select the reactor with `PYJO_REACTOR` before `Pyjo.IOLoop` is loaded:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
PYJO_REACTOR=Pyjo.Reactor.Gevent python myapp.py daemon
|
|
72
|
+
PYJO_REACTOR=Pyjo.Reactor.Gevent python myapp.py prefork
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The reactor runs in the greenlet that starts it, and the other greenlets of the
|
|
76
|
+
program run while it waits for events. A blocking request of the user agent
|
|
77
|
+
waits in the greenlet that makes it, without blocking the others.
|
|
78
|
+
|
|
79
|
+
gevent runs on libev by default. Its loop of libuv needs
|
|
80
|
+
[cffi](https://pypi.org/project/cffi/):
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
pip install Pyjo-Reactor-Gevent cffi
|
|
84
|
+
GEVENT_LOOP=libuv-cffi PYJO_REACTOR=Pyjo.Reactor.Gevent python myapp.py daemon
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
With `gevent.monkey.patch_all()`, the threads of the program are greenlets, so
|
|
88
|
+
each greenlet has a `Pyjo.IOLoop.singleton` of its own, and
|
|
89
|
+
`Pyjo.Server.Prethread` and `Pyjo.IOLoop.ChildProcess` do not work.
|
|
90
|
+
|
|
91
|
+
## Examples
|
|
92
|
+
|
|
93
|
+
`examples/hello_world_gevent.py` runs `Pyjo.HelloWorld` on the server of
|
|
94
|
+
Pyjoyment on gevent until the signal `INT` or `TERM`, in a `with` block, which
|
|
95
|
+
closes the listen sockets of the server at its end:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import os
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# The reactor of Pyjoyment on gevent, selected before Pyjo.IOLoop is loaded
|
|
102
|
+
os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Gevent'
|
|
103
|
+
|
|
104
|
+
import Pyjo.HelloWorld # noqa: E402
|
|
105
|
+
import Pyjo.Server.Daemon # noqa: E402
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# The server of Pyjoyment on gevent, until the signal INT or TERM, which closes its listen sockets at the end of the block
|
|
109
|
+
with Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://*:3000']) as daemon:
|
|
110
|
+
daemon.run()
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```console
|
|
114
|
+
$ python examples/hello_world_gevent.py
|
|
115
|
+
Web application available at http://127.0.0.1:3000
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```console
|
|
119
|
+
$ curl http://127.0.0.1:3000/
|
|
120
|
+
Your Pyjo is working!
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`examples/loopback_gevent.py` is a server of Pyjoyment and its user agent on
|
|
124
|
+
the hub of gevent, a blocking request, which runs the event loop until the
|
|
125
|
+
response, and the class of the reactor and whether it runs on the hub of
|
|
126
|
+
gevent, which Build checks with the wheel:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
import os
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# The reactor of Pyjoyment on gevent, selected before Pyjo.IOLoop is loaded
|
|
133
|
+
os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Gevent'
|
|
134
|
+
|
|
135
|
+
import Pyjo.HelloWorld # noqa: E402
|
|
136
|
+
import Pyjo.IOLoop # noqa: E402
|
|
137
|
+
import Pyjo.Server.Daemon # noqa: E402
|
|
138
|
+
import Pyjo.UserAgent # noqa: E402
|
|
139
|
+
|
|
140
|
+
import gevent # noqa: E402
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
# The server, on a port that the system picks, and the user agent, in the same event loop, which close their sockets at
|
|
144
|
+
# the end of the block
|
|
145
|
+
with (
|
|
146
|
+
Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://127.0.0.1'], silent=True) as daemon,
|
|
147
|
+
Pyjo.UserAgent.new(ioloop=Pyjo.IOLoop.singleton) as ua,
|
|
148
|
+
):
|
|
149
|
+
# A blocking request, which runs the event loop of the server on the hub of gevent until the response
|
|
150
|
+
tx = ua.get(f'http://127.0.0.1:{daemon.start().ports()[0]}/')
|
|
151
|
+
reactor = Pyjo.IOLoop.singleton.reactor
|
|
152
|
+
print(type(reactor).__name__, 'gevent' if getattr(reactor, 'hub', None) is gevent.get_hub() else 'other')
|
|
153
|
+
print(tx.result.code, tx.result.text)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
```console
|
|
157
|
+
$ python examples/loopback_gevent.py
|
|
158
|
+
Pyjo_Reactor_Gevent gevent
|
|
159
|
+
200 Your Pyjo is working!
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`examples/greenlets_gevent.py` is a server of Pyjoyment in the main greenlet,
|
|
163
|
+
along with two greenlets of the application that request it, one with a socket
|
|
164
|
+
of gevent and one with a blocking request of the user agent of Pyjoyment:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
import os
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
# The reactor of Pyjoyment on gevent, selected before Pyjo.IOLoop is loaded
|
|
171
|
+
os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Gevent'
|
|
172
|
+
|
|
173
|
+
import Pyjo.HelloWorld # noqa: E402
|
|
174
|
+
import Pyjo.IOLoop # noqa: E402
|
|
175
|
+
import Pyjo.Server.Daemon # noqa: E402
|
|
176
|
+
import Pyjo.UserAgent # noqa: E402
|
|
177
|
+
|
|
178
|
+
import gevent # noqa: E402
|
|
179
|
+
import gevent.socket # noqa: E402
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# A greenlet with a socket of gevent, which waits in the hub of gevent, where the server of Pyjoyment runs
|
|
183
|
+
def with_socket(port):
|
|
184
|
+
with gevent.socket.create_connection(('127.0.0.1', port)) as sock:
|
|
185
|
+
sock.sendall(b'GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n')
|
|
186
|
+
response = b''
|
|
187
|
+
while b'\r\n' not in response and (chunk := sock.recv(4096)):
|
|
188
|
+
response += chunk
|
|
189
|
+
status = response.split(b'\r\n')[0].decode()
|
|
190
|
+
return f'gevent.socket: {status}'
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
# A greenlet with a blocking request of the user agent of Pyjoyment, whose event loop of its own waits in the greenlet
|
|
194
|
+
def with_user_agent(port):
|
|
195
|
+
with Pyjo.UserAgent.new() as ua:
|
|
196
|
+
tx = ua.get(f'http://127.0.0.1:{port}/')
|
|
197
|
+
return f'Pyjo.UserAgent: {tx.result.code} {tx.result.text}'
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# The server, on a port that the system picks, which runs in the main greenlet until the greenlets are done, and closes its
|
|
201
|
+
# listen sockets at the end of the block
|
|
202
|
+
with Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://127.0.0.1'], silent=True) as daemon:
|
|
203
|
+
port = daemon.start().ports()[0]
|
|
204
|
+
greenlets = [gevent.spawn(with_socket, port), gevent.spawn(with_user_agent, port)]
|
|
205
|
+
gevent.spawn(lambda: (gevent.joinall(greenlets), Pyjo.IOLoop.stop()))
|
|
206
|
+
Pyjo.IOLoop.start()
|
|
207
|
+
for greenlet in greenlets:
|
|
208
|
+
print(greenlet.value)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
```console
|
|
212
|
+
$ python examples/greenlets_gevent.py
|
|
213
|
+
gevent.socket: HTTP/1.1 200 OK
|
|
214
|
+
Pyjo.UserAgent: 200 Your Pyjo is working!
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Documentation
|
|
218
|
+
|
|
219
|
+
The API of the module and the examples are on
|
|
220
|
+
[Read the Docs](https://pyjoyment.readthedocs.io/projects/pyjo-reactor-gevent/en/stable/).
|
|
221
|
+
|
|
222
|
+
## Status
|
|
223
|
+
|
|
224
|
+
A reactor of Pyjoyment in a distribution of its own, as
|
|
225
|
+
[Pyjo-Reactor-Twisted](https://github.com/pyjoyment/Pyjo-Reactor-Twisted) and
|
|
226
|
+
[Pyjo-Reactor-Uvloop](https://github.com/pyjoyment/Pyjo-Reactor-Uvloop).
|