Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
d66aa0f9
Commit
d66aa0f9
authored
Dec 28, 2018
by
Boxiang Sun
Committed by
gsamain
Apr 30, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add nogil tests
parent
d6f02341
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
189 additions
and
0 deletions
+189
-0
nogil_test/basic_test.py
nogil_test/basic_test.py
+21
-0
nogil_test/build_extension.py
nogil_test/build_extension.py
+15
-0
nogil_test/lwan_coro_test.py
nogil_test/lwan_coro_test.py
+20
-0
nogil_test/nogil_extension.py
nogil_test/nogil_extension.py
+14
-0
nogil_test/nogil_extension.pyx
nogil_test/nogil_extension.pyx
+119
-0
No files found.
nogil_test/basic_test.py
0 → 100644
View file @
d66aa0f9
import
subprocess
import
importlib
import
json
def
run
(
env
):
subprocess
.
call
([
'python3'
,
'nogil_extension.py'
,
'build_ext'
,
'--inplace'
],
env
=
env
)
source
=
''
failure_count
=
0
try
:
nogil_extension
=
importlib
.
import_module
(
'nogil_extension'
)
except
ImportError
as
e
:
source
=
str
(
e
)
failure_count
=
1
else
:
source
=
nogil_extension
.
bag
()
if
source
!=
'4.0
\
n
42.0'
:
failure_count
=
1
result_dict
=
{
'failed'
:
failure_count
,
'stdout'
:
source
}
return
result_dict
# print(json.dumps(result_dict))
nogil_test/build_extension.py
0 → 100644
View file @
d66aa0f9
import
subprocess
import
importlib
import
sys
def
build_ext
(
env
):
subprocess
.
check_output
([
'python3'
,
'setup.py'
,
'build_ext'
,
'--inplace'
],
cwd
=
'./cython_lwan_coro/'
,
env
=
env
)
source
=
''
failure_count
=
0
try
:
sys
.
path
.
append
(
'./cython_lwan_coro/'
)
wrapper
=
importlib
.
import_module
(
'wrapper'
)
except
ImportError
as
e
:
failure_count
=
1
source
=
str
(
e
)
print
(
source
)
# Will be captured by check_output
nogil_test/lwan_coro_test.py
0 → 100644
View file @
d66aa0f9
import
io
import
subprocess
import
importlib
import
sys
import
build_extension
from
contextlib
import
redirect_stdout
def
run
(
env
):
# build the extension
build_extension
.
build_ext
(
env
)
source
=
subprocess
.
check_output
([
"python3 -c 'import wrapper; wrapper.main()'"
],
cwd
=
'./cython_lwan_coro/'
,
env
=
env
,
shell
=
True
,
)
failure_count
=
0
if
len
(
source
)
!=
30
:
failure_count
=
1
result_dict
=
{
'failed'
:
failure_count
,
'stdout'
:
source
}
return
result_dict
nogil_test/nogil_extension.py
0 → 100644
View file @
d66aa0f9
from
distutils.core
import
setup
from
distutils.extension
import
Extension
from
Cython.Build
import
cythonize
ext_modules
=
[
Extension
(
'nogil_extension'
,
[
'nogil_extension.pyx'
],
)
]
setup
(
ext_modules
=
cythonize
(
ext_modules
)
)
nogil_test/nogil_extension.pyx
0 → 100644
View file @
d66aa0f9
#cython: language_level = 3
from
cython.parallel
import
parallel
from
libc.stdio
cimport
printf
"""
GOAL: implement nogil option in cdef class (extension types)
and native memory manager (refcount based) that does not
depend on cpython's memory manager and that does not require GIL.
HINT: look at C++ standard library (that works very nicely with Cython)
Cython documentation if here: http://docs.cython.org/en/latest/
Basic usage: http://docs.cython.org/en/latest/src/quickstart/build.html
Tutorial: http://docs.cython.org/en/latest/src/tutorial/cython_tutorial.html
Language: http://docs.cython.org/en/latest/src/userguide/language_basics.html
Extension types: http://docs.cython.org/en/latest/src/userguide/extension_types.html
Extension Types are the "pure cython" classes that I want to be able to
use without depending on cpython GIL (and in essence runtime, memory manager, etc.)
Cython memory allocation: http://docs.cython.org/en/latest/src/tutorial/memory_allocation.html
Parallelism: http://docs.cython.org/en/latest/src/userguide/parallelism.html
Explains how nogil is posisble in cython for anything that
only relies on C libraries that are multi-threaded
"""
# cdef class SomeMemory:
cdef
class
SomeMemory
nogil
:
"""
This is a cdef class which is also called
a extensino type. It is a kind of C struct
that also acts as a python object.
We would like to be able to define "nogil"
extension types:
cdef class SomeMemory nogil:
where all methods are "nogil" and memory
allocation does not depend on python runtime
"""
cdef
int
a
;
cdef
double
b
;
cdef
void
cinit
(
self
,
a
,
b
)
nogil
:
# self.a = a
# self.b = b
pass
cdef
void
cdealloc
(
self
)
nogil
:
pass
cdef
void
foo1
(
self
)
nogil
:
"""
It is possible to define native C/Cython methods
that release the GIL (cool...)
"""
self
.
a
+=
3
cdef
void
foo2
(
self
)
nogil
:
"""
It is possible to define native C/Cython methods
that release the GIL (cool...)
"""
self
.
a
=
42
# Not allowed to define pure Python function in the extension type with nogil option now
# since we want this extension type is CPython free
# def baz(self):
# """
# It is also possible to define standard python
# methods
# """
# pass
# cdef bar(): # it is currently impossible to release GIL
cdef
double
bar1
()
nogil
:
# yet this is what we would like to
"""
This is a pure "cython method" which we would like to
be able to declare with nogil option but this requires
to first introduce the concept of nogil in cdef class
"""
cdef
SomeMemory
o1
=
SomeMemory
(
1
,
1.0
)
# Call init and get an object
o1
.
foo1
()
return
o1
.
a
cdef
double
bar2
()
nogil
:
# yet this is what we would like to
cdef
SomeMemory
o2
=
SomeMemory
(
2
,
2.0
)
o2
.
foo2
()
return
o2
.
a
cpdef
baz1
():
"""
This method is both callable from python and pure "cython".
It can call both cdef methods and usual python functions
"""
return
bar1
()
cpdef
baz2
():
"""
This method is both callable from python and pure "cython".
It can call both cdef methods and usual python functions
"""
return
bar2
()
def
bag
():
return
str
(
baz1
())
+
'
\
n
'
+
str
(
baz2
())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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