Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bliss
python-handel
Commits
d040c8d5
Commit
d040c8d5
authored
Oct 23, 2017
by
Vincent Michel
Browse files
Add gevent compatibility
parent
3c491e17
Pipeline
#1252
passed with stages
in 2 minutes and 34 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
d040c8d5
...
...
@@ -22,6 +22,7 @@ Run requirements:
-
cffi
-
numpy
-
gevent (optional)
Test requirements:
...
...
@@ -64,6 +65,16 @@ Example usage:
array
([
13260
,
52275
,
256
,
...,
0
,
0
,
0
],
dtype
=
uint32
)
```
Gevent mode
-----------
Make the interface gevent-compatible using:
```
python
>>>
from
handel.gevent
import
patch
>>>
patch
()
```
Tests
-----
...
...
handel/gevent.py
0 → 100644
View file @
d040c8d5
"""Provide gevent compatibility.
Usage:
>>> from handel.gevent import patch
>>> patch()
"""
from
__future__
import
absolute_import
from
functools
import
wraps
from
gevent
import
threadpool
from
.interface
import
handel
# Green pool
# The 'maxsize=1' argument provides implicit locking
POOL
=
threadpool
.
ThreadPool
(
maxsize
=
1
)
# Green decorator
def
green
(
func
,
pool
=
POOL
):
"""Make a given function gevent-compatible by running
it in a gevent threadpool."""
@
wraps
(
func
)
def
wrapper
(
*
args
,
**
kwargs
):
return
pool
.
apply
(
func
,
args
,
kwargs
)
return
wrapper
# Patch function
def
patch
():
"""Provide gevent compatibility.
Usage:
>>> from handel.gevent import patch
>>> patch()
"""
for
name
in
dir
(
handel
):
if
name
.
startswith
(
'xia'
):
func
=
getattr
(
handel
,
name
)
setattr
(
handel
,
name
,
green
(
func
))
scripts/handel-server
View file @
d040c8d5
...
...
@@ -21,11 +21,12 @@ import sys
import
zerorpc
import
msgpack_numpy
import
handel.gevent
import
handel.interface
as
hi
# Patching
handel
.
gevent
.
patch
()
msgpack_numpy
.
patch
()
...
...
setup.py
View file @
d040c8d5
...
...
@@ -13,7 +13,7 @@ setup(
packages
=
[
'handel'
],
install_requires
=
[
'cffi'
,
'numpy'
],
setup_requires
=
[
'pytest-runner'
,
'pytest'
]
if
TESTING
else
[],
tests_require
=
[
'pytest-cov'
,
'mock'
],
tests_require
=
[
'pytest-cov'
,
'mock'
,
'gevent'
],
description
=
"Python binding for the Handel library"
,
long_description
=
long_description
,
...
...
tests/test_gevent.py
0 → 100644
View file @
d040c8d5
import
mock
def
test_gevent_compatibility
():
with
mock
.
patch
(
'cffi.FFI.dlopen'
)
as
dlopen
:
# Declare xiaSomeFunction
handel
=
dlopen
.
return_value
original
=
handel
.
xiaSomeFunction
original
.
__name__
=
'xiaSomeFunction'
original
.
return_value
=
'Some result'
# Patching
from
handel.gevent
import
patch
from
handel
import
interface
assert
patch
()
is
None
# Checking
assert
interface
.
handel
.
xiaSomeFunction
.
__name__
==
'xiaSomeFunction'
assert
interface
.
handel
.
xiaSomeFunction
(
1
,
a
=
2
)
is
'Some result'
original
.
assert_called_once_with
(
1
,
a
=
2
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment