Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
2bcae708
Kaydet (Commit)
2bcae708
authored
Kas 13, 2013
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
asyncio: Fix from Anthony Baire for CPython issue 19566 (replaces earlier fix).
üst
be3c2fea
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
42 deletions
+60
-42
unix_events.py
Lib/asyncio/unix_events.py
+41
-28
test_events.py
Lib/test/test_asyncio/test_events.py
+3
-1
test_unix_events.py
Lib/test/test_asyncio/test_unix_events.py
+16
-13
No files found.
Lib/asyncio/unix_events.py
Dosyayı görüntüle @
2bcae708
...
@@ -440,10 +440,13 @@ class AbstractChildWatcher:
...
@@ -440,10 +440,13 @@ class AbstractChildWatcher:
raise
NotImplementedError
()
raise
NotImplementedError
()
def
set
_loop
(
self
,
loop
):
def
attach
_loop
(
self
,
loop
):
"""
Reattach the watcher to another
event loop.
"""
Attach the watcher to an
event loop.
Note: loop may be None
If the watcher was previously attached to an event loop, then it is
first detached before attaching to the new loop.
Note: loop may be None.
"""
"""
raise
NotImplementedError
()
raise
NotImplementedError
()
...
@@ -467,15 +470,11 @@ class AbstractChildWatcher:
...
@@ -467,15 +470,11 @@ class AbstractChildWatcher:
class
BaseChildWatcher
(
AbstractChildWatcher
):
class
BaseChildWatcher
(
AbstractChildWatcher
):
def
__init__
(
self
,
loop
):
def
__init__
(
self
):
self
.
_loop
=
None
self
.
_loop
=
None
self
.
_callbacks
=
{}
self
.
set_loop
(
loop
)
def
close
(
self
):
def
close
(
self
):
self
.
set_loop
(
None
)
self
.
attach_loop
(
None
)
self
.
_callbacks
.
clear
()
def
_do_waitpid
(
self
,
expected_pid
):
def
_do_waitpid
(
self
,
expected_pid
):
raise
NotImplementedError
()
raise
NotImplementedError
()
...
@@ -483,7 +482,7 @@ class BaseChildWatcher(AbstractChildWatcher):
...
@@ -483,7 +482,7 @@ class BaseChildWatcher(AbstractChildWatcher):
def
_do_waitpid_all
(
self
):
def
_do_waitpid_all
(
self
):
raise
NotImplementedError
()
raise
NotImplementedError
()
def
set
_loop
(
self
,
loop
):
def
attach
_loop
(
self
,
loop
):
assert
loop
is
None
or
isinstance
(
loop
,
events
.
AbstractEventLoop
)
assert
loop
is
None
or
isinstance
(
loop
,
events
.
AbstractEventLoop
)
if
self
.
_loop
is
not
None
:
if
self
.
_loop
is
not
None
:
...
@@ -497,13 +496,6 @@ class BaseChildWatcher(AbstractChildWatcher):
...
@@ -497,13 +496,6 @@ class BaseChildWatcher(AbstractChildWatcher):
# during the switch.
# during the switch.
self
.
_do_waitpid_all
()
self
.
_do_waitpid_all
()
def
remove_child_handler
(
self
,
pid
):
try
:
del
self
.
_callbacks
[
pid
]
return
True
except
KeyError
:
return
False
def
_sig_chld
(
self
):
def
_sig_chld
(
self
):
try
:
try
:
self
.
_do_waitpid_all
()
self
.
_do_waitpid_all
()
...
@@ -535,6 +527,14 @@ class SafeChildWatcher(BaseChildWatcher):
...
@@ -535,6 +527,14 @@ class SafeChildWatcher(BaseChildWatcher):
big number of children (O(n) each time SIGCHLD is raised)
big number of children (O(n) each time SIGCHLD is raised)
"""
"""
def
__init__
(
self
):
super
()
.
__init__
()
self
.
_callbacks
=
{}
def
close
(
self
):
self
.
_callbacks
.
clear
()
super
()
.
close
()
def
__enter__
(
self
):
def
__enter__
(
self
):
return
self
return
self
...
@@ -547,6 +547,13 @@ class SafeChildWatcher(BaseChildWatcher):
...
@@ -547,6 +547,13 @@ class SafeChildWatcher(BaseChildWatcher):
# Prevent a race condition in case the child is already terminated.
# Prevent a race condition in case the child is already terminated.
self
.
_do_waitpid
(
pid
)
self
.
_do_waitpid
(
pid
)
def
remove_child_handler
(
self
,
pid
):
try
:
del
self
.
_callbacks
[
pid
]
return
True
except
KeyError
:
return
False
def
_do_waitpid_all
(
self
):
def
_do_waitpid_all
(
self
):
for
pid
in
list
(
self
.
_callbacks
):
for
pid
in
list
(
self
.
_callbacks
):
...
@@ -592,17 +599,17 @@ class FastChildWatcher(BaseChildWatcher):
...
@@ -592,17 +599,17 @@ class FastChildWatcher(BaseChildWatcher):
There is no noticeable overhead when handling a big number of children
There is no noticeable overhead when handling a big number of children
(O(1) each time a child terminates).
(O(1) each time a child terminates).
"""
"""
def
__init__
(
self
,
loop
):
def
__init__
(
self
):
super
()
.
__init__
()
self
.
_callbacks
=
{}
self
.
_lock
=
threading
.
Lock
()
self
.
_lock
=
threading
.
Lock
()
self
.
_zombies
=
{}
self
.
_zombies
=
{}
self
.
_forks
=
0
self
.
_forks
=
0
# Call base class constructor last because it calls back into
# the subclass (set_loop() calls _do_waitpid()).
super
()
.
__init__
(
loop
)
def
close
(
self
):
def
close
(
self
):
s
uper
()
.
close
()
s
elf
.
_callbacks
.
clear
()
self
.
_zombies
.
clear
()
self
.
_zombies
.
clear
()
super
()
.
close
()
def
__enter__
(
self
):
def
__enter__
(
self
):
with
self
.
_lock
:
with
self
.
_lock
:
...
@@ -643,6 +650,13 @@ class FastChildWatcher(BaseChildWatcher):
...
@@ -643,6 +650,13 @@ class FastChildWatcher(BaseChildWatcher):
else
:
else
:
callback
(
pid
,
returncode
,
*
args
)
callback
(
pid
,
returncode
,
*
args
)
def
remove_child_handler
(
self
,
pid
):
try
:
del
self
.
_callbacks
[
pid
]
return
True
except
KeyError
:
return
False
def
_do_waitpid_all
(
self
):
def
_do_waitpid_all
(
self
):
# Because of signal coalescing, we must keep calling waitpid() as
# Because of signal coalescing, we must keep calling waitpid() as
# long as we're able to reap a child.
# long as we're able to reap a child.
...
@@ -687,25 +701,24 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
...
@@ -687,25 +701,24 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
def
_init_watcher
(
self
):
def
_init_watcher
(
self
):
with
events
.
_lock
:
with
events
.
_lock
:
if
self
.
_watcher
is
None
:
# pragma: no branch
if
self
.
_watcher
is
None
:
# pragma: no branch
self
.
_watcher
=
SafeChildWatcher
()
if
isinstance
(
threading
.
current_thread
(),
if
isinstance
(
threading
.
current_thread
(),
threading
.
_MainThread
):
threading
.
_MainThread
):
self
.
_watcher
=
SafeChildWatcher
(
self
.
_local
.
_loop
)
self
.
_watcher
.
attach_loop
(
self
.
_local
.
_loop
)
else
:
self
.
_watcher
=
SafeChildWatcher
(
None
)
def
set_event_loop
(
self
,
loop
):
def
set_event_loop
(
self
,
loop
):
"""Set the event loop.
"""Set the event loop.
As a side effect, if a child watcher was set before, then calling
As a side effect, if a child watcher was set before, then calling
.set_event_loop() from the main thread will call .
set_loop(loop) on the
.set_event_loop() from the main thread will call .
attach_loop(loop) on
child watcher.
the
child watcher.
"""
"""
super
()
.
set_event_loop
(
loop
)
super
()
.
set_event_loop
(
loop
)
if
self
.
_watcher
is
not
None
and
\
if
self
.
_watcher
is
not
None
and
\
isinstance
(
threading
.
current_thread
(),
threading
.
_MainThread
):
isinstance
(
threading
.
current_thread
(),
threading
.
_MainThread
):
self
.
_watcher
.
set
_loop
(
loop
)
self
.
_watcher
.
attach
_loop
(
loop
)
def
get_child_watcher
(
self
):
def
get_child_watcher
(
self
):
"""Get the child watcher
"""Get the child watcher
...
...
Lib/test/test_asyncio/test_events.py
Dosyayı görüntüle @
2bcae708
...
@@ -1311,7 +1311,9 @@ else:
...
@@ -1311,7 +1311,9 @@ else:
class
UnixEventLoopTestsMixin
(
EventLoopTestsMixin
):
class
UnixEventLoopTestsMixin
(
EventLoopTestsMixin
):
def
setUp
(
self
):
def
setUp
(
self
):
super
()
.
setUp
()
super
()
.
setUp
()
events
.
set_child_watcher
(
unix_events
.
SafeChildWatcher
(
self
.
loop
))
watcher
=
unix_events
.
SafeChildWatcher
()
watcher
.
attach_loop
(
self
.
loop
)
events
.
set_child_watcher
(
watcher
)
def
tearDown
(
self
):
def
tearDown
(
self
):
events
.
set_child_watcher
(
None
)
events
.
set_child_watcher
(
None
)
...
...
Lib/test/test_asyncio/test_unix_events.py
Dosyayı görüntüle @
2bcae708
...
@@ -687,7 +687,7 @@ class AbstractChildWatcherTests(unittest.TestCase):
...
@@ -687,7 +687,7 @@ class AbstractChildWatcherTests(unittest.TestCase):
self
.
assertRaises
(
self
.
assertRaises
(
NotImplementedError
,
watcher
.
remove_child_handler
,
f
)
NotImplementedError
,
watcher
.
remove_child_handler
,
f
)
self
.
assertRaises
(
self
.
assertRaises
(
NotImplementedError
,
watcher
.
set
_loop
,
f
)
NotImplementedError
,
watcher
.
attach
_loop
,
f
)
self
.
assertRaises
(
self
.
assertRaises
(
NotImplementedError
,
watcher
.
close
)
NotImplementedError
,
watcher
.
close
)
self
.
assertRaises
(
self
.
assertRaises
(
...
@@ -700,7 +700,7 @@ class BaseChildWatcherTests(unittest.TestCase):
...
@@ -700,7 +700,7 @@ class BaseChildWatcherTests(unittest.TestCase):
def
test_not_implemented
(
self
):
def
test_not_implemented
(
self
):
f
=
unittest
.
mock
.
Mock
()
f
=
unittest
.
mock
.
Mock
()
watcher
=
unix_events
.
BaseChildWatcher
(
None
)
watcher
=
unix_events
.
BaseChildWatcher
()
self
.
assertRaises
(
self
.
assertRaises
(
NotImplementedError
,
watcher
.
_do_waitpid
,
f
)
NotImplementedError
,
watcher
.
_do_waitpid
,
f
)
...
@@ -720,10 +720,13 @@ class ChildWatcherTestsMixin:
...
@@ -720,10 +720,13 @@ class ChildWatcherTestsMixin:
with
unittest
.
mock
.
patch
.
object
(
with
unittest
.
mock
.
patch
.
object
(
self
.
loop
,
"add_signal_handler"
)
as
self
.
m_add_signal_handler
:
self
.
loop
,
"add_signal_handler"
)
as
self
.
m_add_signal_handler
:
self
.
watcher
=
self
.
create_watcher
(
self
.
loop
)
self
.
watcher
=
self
.
create_watcher
()
self
.
watcher
.
attach_loop
(
self
.
loop
)
def
tearDown
(
self
):
def
cleanup
():
ChildWatcherTestsMixin
.
instance
=
None
ChildWatcherTestsMixin
.
instance
=
None
self
.
addCleanup
(
cleanup
)
def
waitpid
(
pid
,
flags
):
def
waitpid
(
pid
,
flags
):
self
=
ChildWatcherTestsMixin
.
instance
self
=
ChildWatcherTestsMixin
.
instance
...
@@ -1334,7 +1337,7 @@ class ChildWatcherTestsMixin:
...
@@ -1334,7 +1337,7 @@ class ChildWatcherTestsMixin:
self
.
loop
,
self
.
loop
,
"add_signal_handler"
)
as
m_new_add_signal_handler
:
"add_signal_handler"
)
as
m_new_add_signal_handler
:
self
.
watcher
.
set
_loop
(
self
.
loop
)
self
.
watcher
.
attach
_loop
(
self
.
loop
)
m_old_remove_signal_handler
.
assert_called_once_with
(
m_old_remove_signal_handler
.
assert_called_once_with
(
signal
.
SIGCHLD
)
signal
.
SIGCHLD
)
...
@@ -1375,7 +1378,7 @@ class ChildWatcherTestsMixin:
...
@@ -1375,7 +1378,7 @@ class ChildWatcherTestsMixin:
with
unittest
.
mock
.
patch
.
object
(
with
unittest
.
mock
.
patch
.
object
(
old_loop
,
"remove_signal_handler"
)
as
m_remove_signal_handler
:
old_loop
,
"remove_signal_handler"
)
as
m_remove_signal_handler
:
self
.
watcher
.
set
_loop
(
None
)
self
.
watcher
.
attach
_loop
(
None
)
m_remove_signal_handler
.
assert_called_once_with
(
m_remove_signal_handler
.
assert_called_once_with
(
signal
.
SIGCHLD
)
signal
.
SIGCHLD
)
...
@@ -1395,7 +1398,7 @@ class ChildWatcherTestsMixin:
...
@@ -1395,7 +1398,7 @@ class ChildWatcherTestsMixin:
with
unittest
.
mock
.
patch
.
object
(
with
unittest
.
mock
.
patch
.
object
(
self
.
loop
,
"add_signal_handler"
)
as
m_add_signal_handler
:
self
.
loop
,
"add_signal_handler"
)
as
m_add_signal_handler
:
self
.
watcher
.
set
_loop
(
self
.
loop
)
self
.
watcher
.
attach
_loop
(
self
.
loop
)
m_add_signal_handler
.
assert_called_once_with
(
m_add_signal_handler
.
assert_called_once_with
(
signal
.
SIGCHLD
,
self
.
watcher
.
_sig_chld
)
signal
.
SIGCHLD
,
self
.
watcher
.
_sig_chld
)
...
@@ -1457,13 +1460,13 @@ class ChildWatcherTestsMixin:
...
@@ -1457,13 +1460,13 @@ class ChildWatcherTestsMixin:
class
SafeChildWatcherTests
(
ChildWatcherTestsMixin
,
unittest
.
TestCase
):
class
SafeChildWatcherTests
(
ChildWatcherTestsMixin
,
unittest
.
TestCase
):
def
create_watcher
(
self
,
loop
):
def
create_watcher
(
self
):
return
unix_events
.
SafeChildWatcher
(
loop
)
return
unix_events
.
SafeChildWatcher
()
class
FastChildWatcherTests
(
ChildWatcherTestsMixin
,
unittest
.
TestCase
):
class
FastChildWatcherTests
(
ChildWatcherTestsMixin
,
unittest
.
TestCase
):
def
create_watcher
(
self
,
loop
):
def
create_watcher
(
self
):
return
unix_events
.
FastChildWatcher
(
loop
)
return
unix_events
.
FastChildWatcher
()
class
PolicyTests
(
unittest
.
TestCase
):
class
PolicyTests
(
unittest
.
TestCase
):
...
@@ -1485,7 +1488,7 @@ class PolicyTests(unittest.TestCase):
...
@@ -1485,7 +1488,7 @@ class PolicyTests(unittest.TestCase):
def
test_get_child_watcher_after_set
(
self
):
def
test_get_child_watcher_after_set
(
self
):
policy
=
self
.
create_policy
()
policy
=
self
.
create_policy
()
watcher
=
unix_events
.
FastChildWatcher
(
None
)
watcher
=
unix_events
.
FastChildWatcher
()
policy
.
set_child_watcher
(
watcher
)
policy
.
set_child_watcher
(
watcher
)
self
.
assertIs
(
policy
.
_watcher
,
watcher
)
self
.
assertIs
(
policy
.
_watcher
,
watcher
)
...
...
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