Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_fork1.ForkTest Class Reference
Inheritance diagram for test.test_fork1.ForkTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_fork1.ForkTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def wait_impl
def test_threaded_import_lock_fork
def test_nested_import_lock_fork
def setUp
def f
def test_wait

Public Attributes

 alive
 stop

Detailed Description

Definition at line 19 of file test_fork1.py.


Member Function Documentation

def test.fork_wait.ForkWait.f (   self,
  id 
) [inherited]

Definition at line 26 of file fork_wait.py.

00026 
00027     def f(self, id):
00028         while not self.stop:
00029             self.alive[id] = os.getpid()
00030             try:
00031                 time.sleep(SHORTSLEEP)
00032             except IOError:
00033                 pass

Here is the caller graph for this function:

def test.fork_wait.ForkWait.setUp (   self) [inherited]

Definition at line 22 of file fork_wait.py.

00022 
00023     def setUp(self):
00024         self.alive = {}
00025         self.stop = 0

Here is the caller graph for this function:

Check fork() in main thread works while the main thread is doing an import

Definition at line 74 of file test_fork1.py.

00074 
00075     def test_nested_import_lock_fork(self):
00076         """Check fork() in main thread works while the main thread is doing an import"""
00077         # Issue 9573: this used to trigger RuntimeError in the child process
00078         def fork_with_import_lock(level):
00079             release = 0
00080             in_child = False
00081             try:
00082                 try:
00083                     for i in range(level):
00084                         imp.acquire_lock()
00085                         release += 1
00086                     pid = os.fork()
00087                     in_child = not pid
00088                 finally:
00089                     for i in range(release):
00090                         imp.release_lock()
00091             except RuntimeError:
00092                 if in_child:
00093                     if verbose > 1:
00094                         print("RuntimeError in child")
00095                     os._exit(1)
00096                 raise
00097             if in_child:
00098                 os._exit(0)
00099             self.wait_impl(pid)
00100 
00101         # Check this works with various levels of nested
00102         # import in the main thread
00103         for level in range(5):
00104             fork_with_import_lock(level)
00105 

Here is the call graph for this function:

Check fork() in main thread works while a subthread is doing an import

Definition at line 32 of file test_fork1.py.

00032 
00033     def test_threaded_import_lock_fork(self):
00034         """Check fork() in main thread works while a subthread is doing an import"""
00035         import_started = threading.Event()
00036         fake_module_name = "fake test module"
00037         partial_module = "partial"
00038         complete_module = "complete"
00039         def importer():
00040             imp.acquire_lock()
00041             sys.modules[fake_module_name] = partial_module
00042             import_started.set()
00043             time.sleep(0.01) # Give the other thread time to try and acquire.
00044             sys.modules[fake_module_name] = complete_module
00045             imp.release_lock()
00046         t = threading.Thread(target=importer)
00047         t.start()
00048         import_started.wait()
00049         pid = os.fork()
00050         try:
00051             # PyOS_BeforeFork should have waited for the import to complete
00052             # before forking, so the child can recreate the import lock
00053             # correctly, but also won't see a partially initialised module
00054             if not pid:
00055                 m = __import__(fake_module_name)
00056                 if m == complete_module:
00057                     os._exit(0)
00058                 else:
00059                     if verbose > 1:
00060                         print("Child encountered partial module")
00061                     os._exit(1)
00062             else:
00063                 t.join()
00064                 # Exitcode 1 means the child got a partial module (bad.) No
00065                 # exitcode (but a hang, which manifests as 'got pid 0')
00066                 # means the child deadlocked (also bad.)
00067                 self.wait_impl(pid)
00068         finally:
00069             try:
00070                 os.kill(pid, signal.SIGKILL)
00071             except OSError:
00072                 pass
00073 

Here is the call graph for this function:

def test.fork_wait.ForkWait.test_wait (   self) [inherited]

Definition at line 46 of file fork_wait.py.

00046 
00047     def test_wait(self):
00048         for i in range(NUM_THREADS):
00049             _thread.start_new(self.f, (i,))
00050 
00051         time.sleep(LONGSLEEP)
00052 
00053         a = sorted(self.alive.keys())
00054         self.assertEqual(a, list(range(NUM_THREADS)))
00055 
00056         prefork_lives = self.alive.copy()
00057 
00058         if sys.platform in ['unixware7']:
00059             cpid = os.fork1()
00060         else:
00061             cpid = os.fork()
00062 
00063         if cpid == 0:
00064             # Child
00065             time.sleep(LONGSLEEP)
00066             n = 0
00067             for key in self.alive:
00068                 if self.alive[key] != prefork_lives[key]:
00069                     n += 1
00070             os._exit(n)
00071         else:
00072             # Parent
00073             self.wait_impl(cpid)
00074             # Tell threads to die
00075             self.stop = 1
00076             time.sleep(2*SHORTSLEEP) # Wait for threads to die

Here is the call graph for this function:

def test.test_fork1.ForkTest.wait_impl (   self,
  cpid 
)

Reimplemented from test.fork_wait.ForkWait.

Definition at line 20 of file test_fork1.py.

00020 
00021     def wait_impl(self, cpid):
00022         for i in range(10):
00023             # waitpid() shouldn't hang, but some of the buildbots seem to hang
00024             # in the forking tests.  This is an attempt to fix the problem.
00025             spid, status = os.waitpid(cpid, os.WNOHANG)
00026             if spid == cpid:
00027                 break
00028             time.sleep(1.0)
00029 
00030         self.assertEqual(spid, cpid)
00031         self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))

Here is the call graph for this function:


Member Data Documentation

Definition at line 23 of file fork_wait.py.

Definition at line 24 of file fork_wait.py.


The documentation for this class was generated from the following file: