Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
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
Kirill Smelkov
gevent
Commits
9948bd20
Commit
9948bd20
authored
Jul 08, 2015
by
Jason Madden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize gevent.local to create fewer weakrefs. Fix leakcheck on the new local test.
parent
1192b1c6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
39 deletions
+51
-39
gevent/local.py
gevent/local.py
+33
-24
greentest/test__local.py
greentest/test__local.py
+18
-15
No files found.
gevent/local.py
View file @
9948bd20
...
@@ -136,6 +136,10 @@ from gevent.lock import RLock
...
@@ -136,6 +136,10 @@ from gevent.lock import RLock
__all__
=
[
"local"
]
__all__
=
[
"local"
]
class
_wrefdict
(
dict
):
"""A dict that can be weak referenced"""
class
_localimpl
(
object
):
class
_localimpl
(
object
):
"""A class managing thread-local dicts"""
"""A class managing thread-local dicts"""
__slots__
=
'key'
,
'dicts'
,
'localargs'
,
'locallock'
,
'__weakref__'
__slots__
=
'key'
,
'dicts'
,
'localargs'
,
'locallock'
,
'__weakref__'
...
@@ -146,7 +150,7 @@ class _localimpl(object):
...
@@ -146,7 +150,7 @@ class _localimpl(object):
# a "real" attribute.
# a "real" attribute.
self
.
key
=
'_threading_local._localimpl.'
+
str
(
id
(
self
))
self
.
key
=
'_threading_local._localimpl.'
+
str
(
id
(
self
))
# { id(Thread) -> (ref(Thread), thread-local dict) }
# { id(Thread) -> (ref(Thread), thread-local dict) }
self
.
dicts
=
{}
self
.
dicts
=
_wrefdict
()
def
get_dict
(
self
):
def
get_dict
(
self
):
"""Return the dict for the current thread. Raises KeyError if none
"""Return the dict for the current thread. Raises KeyError if none
...
@@ -161,6 +165,13 @@ class _localimpl(object):
...
@@ -161,6 +165,13 @@ class _localimpl(object):
thread
=
getcurrent
()
thread
=
getcurrent
()
idt
=
id
(
thread
)
idt
=
id
(
thread
)
# If we are working with a gevent.greenlet.Greenlet, we can
# pro-actively clear out with a link. Use rawlink to avoid
# spawning any more greenlets
try
:
rawlink
=
thread
.
rawlink
except
AttributeError
:
# Otherwise we need to do it with weak refs
def
local_deleted
(
_
,
key
=
key
):
def
local_deleted
(
_
,
key
=
key
):
# When the localimpl is deleted, remove the thread attribute.
# When the localimpl is deleted, remove the thread attribute.
thread
=
wrthread
()
thread
=
wrthread
()
...
@@ -178,19 +189,17 @@ class _localimpl(object):
...
@@ -178,19 +189,17 @@ class _localimpl(object):
wrlocal
=
ref
(
self
,
local_deleted
)
wrlocal
=
ref
(
self
,
local_deleted
)
wrthread
=
ref
(
thread
,
thread_deleted
)
wrthread
=
ref
(
thread
,
thread_deleted
)
thread
.
__dict__
[
key
]
=
wrlocal
thread
.
__dict__
[
key
]
=
wrlocal
self
.
dicts
[
idt
]
=
wrthread
,
localdict
# If we are working with a gevent.greenlet.Greenlet, we can
# pro-actively clear out with a link. Use rawlink to avoid
# spawning any more greenlets
try
:
rawlink
=
thread
.
rawlink
except
AttributeError
:
pass
else
:
else
:
def
greenlet_dead
(
_
):
wrdicts
=
ref
(
self
.
dicts
)
thread_deleted
(
_
)
rawlink
(
greenlet_dead
)
def
clear
(
_
):
dicts
=
wrdicts
()
if
dicts
:
dicts
.
pop
(
idt
,
None
)
rawlink
(
clear
)
wrthread
=
None
self
.
dicts
[
idt
]
=
wrthread
,
localdict
return
localdict
return
localdict
...
...
greentest/test__local.py
View file @
9948bd20
...
@@ -21,6 +21,22 @@ class A(local):
...
@@ -21,6 +21,22 @@ class A(local):
class
Obj
(
object
):
class
Obj
(
object
):
pass
pass
# These next two classes have to be global to avoid the leakchecks
deleted_sentinels
=
[]
created_sentinels
=
[]
class
Sentinel
(
object
):
def
__del__
(
self
):
deleted_sentinels
.
append
(
id
(
self
))
class
MyLocal
(
local
):
def
__init__
(
self
):
local
.
__init__
(
self
)
self
.
sentinel
=
Sentinel
()
created_sentinels
.
append
(
id
(
self
.
sentinel
))
class
GeventLocalTestCase
(
greentest
.
TestCase
):
class
GeventLocalTestCase
(
greentest
.
TestCase
):
...
@@ -61,27 +77,14 @@ class GeventLocalTestCase(greentest.TestCase):
...
@@ -61,27 +77,14 @@ class GeventLocalTestCase(greentest.TestCase):
def
test_locals_collected_when_greenlet_dead_but_still_referenced
(
self
):
def
test_locals_collected_when_greenlet_dead_but_still_referenced
(
self
):
# https://github.com/gevent/gevent/issues/387
# https://github.com/gevent/gevent/issues/387
import
gevent
import
gevent
deleted_sentinels
=
[]
created_sentinels
=
[]
class
Sentinel
(
object
):
def
__del__
(
self
):
deleted_sentinels
.
append
(
id
(
self
))
class
MyLocal
(
local
):
def
__init__
(
self
):
local
.
__init__
(
self
)
self
.
sentinel
=
Sentinel
()
created_sentinels
.
append
(
id
(
self
.
sentinel
))
my_local
=
MyLocal
()
my_local
=
MyLocal
()
my_local
.
sentinel
=
None
my_local
.
sentinel
=
None
if
greentest
.
PYPY
:
if
greentest
.
PYPY
:
import
gc
import
gc
gc
.
collect
()
gc
.
collect
()
# drop the original
del
created_sentinels
[:]
created_sentinels
.
pop
()
del
deleted_sentinels
[:]
deleted_sentinels
.
pop
()
def
demonstrate_my_local
():
def
demonstrate_my_local
():
# Get the important parts
# Get the important parts
...
...
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