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
0723d2c7
Kaydet (Commit)
0723d2c7
authored
Agu 22, 2010
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add an import lock test for multithreaded circular imports.
(part of #9657)
üst
8d18907a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
2 deletions
+60
-2
test_threaded_import.py
Lib/test/test_threaded_import.py
+60
-2
No files found.
Lib/test/test_threaded_import.py
Dosyayı görüntüle @
0723d2c7
...
...
@@ -5,12 +5,15 @@
# complains several times about module random having no attribute
# randrange, and then Python hangs.
import
os
import
imp
import
sys
import
time
import
shutil
import
unittest
from
test.support
import
verbose
,
TestFailed
,
import_module
,
run_unittest
from
test.support
import
verbose
,
import_module
,
run_unittest
,
TESTFN
thread
=
import_module
(
'_thread'
)
threading
=
import_module
(
'threading'
)
def
task
(
N
,
done
,
done_tasks
,
errors
):
try
:
...
...
@@ -32,6 +35,25 @@ def task(N, done, done_tasks, errors):
if
finished
:
done
.
release
()
# Create a circular import structure: A -> C -> B -> D -> A
# NOTE: `time` is already loaded and therefore doesn't threaten to deadlock.
circular_imports_modules
=
{
'A'
:
"""if 1:
import time
time.sleep(
%(delay)
s)
x = 'a'
import C
"""
,
'B'
:
"""if 1:
import time
time.sleep(
%(delay)
s)
x = 'b'
import D
"""
,
'C'
:
"""import B"""
,
'D'
:
"""import A"""
,
}
class
Finder
:
"""A dummy finder to detect concurrent access to its find_module()
...
...
@@ -144,10 +166,46 @@ class ThreadedImportTests(unittest.TestCase):
import
test.threaded_import_hangers
self
.
assertFalse
(
test
.
threaded_import_hangers
.
errors
)
def
test_circular_imports
(
self
):
# The goal of this test is to exercise implementations of the import
# lock which use a per-module lock, rather than a global lock.
# In these implementations, there is a possible deadlock with
# circular imports, for example:
# - thread 1 imports A (grabbing the lock for A) which imports B
# - thread 2 imports B (grabbing the lock for B) which imports A
# Such implementations should be able to detect such situations and
# resolve them one way or the other, without freezing.
# NOTE: our test constructs a slightly less trivial import cycle,
# in order to better stress the deadlock avoidance mechanism.
delay
=
0.5
os
.
mkdir
(
TESTFN
)
self
.
addCleanup
(
shutil
.
rmtree
,
TESTFN
)
sys
.
path
.
insert
(
0
,
TESTFN
)
self
.
addCleanup
(
sys
.
path
.
remove
,
TESTFN
)
for
name
,
contents
in
circular_imports_modules
.
items
():
contents
=
contents
%
{
'delay'
:
delay
}
with
open
(
os
.
path
.
join
(
TESTFN
,
name
+
".py"
),
"wb"
)
as
f
:
f
.
write
(
contents
.
encode
(
'utf-8'
))
self
.
addCleanup
(
sys
.
modules
.
pop
,
name
,
None
)
results
=
[]
def
import_ab
():
import
A
results
.
append
(
getattr
(
A
,
'x'
,
None
))
def
import_ba
():
import
B
results
.
append
(
getattr
(
B
,
'x'
,
None
))
t1
=
threading
.
Thread
(
target
=
import_ab
)
t2
=
threading
.
Thread
(
target
=
import_ba
)
t1
.
start
()
t2
.
start
()
t1
.
join
()
t2
.
join
()
self
.
assertEqual
(
set
(
results
),
{
'a'
,
'b'
})
def
test_main
():
run_unittest
(
ThreadedImportTests
)
if
__name__
==
"__main__"
:
test_main
()
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