本文整理汇总了Python中thread.join方法的典型用法代码示例。如果您正苦于以下问题:Python thread.join方法的具体用法?Python thread.join怎么用?Python thread.join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类thread
的用法示例。
在下文中一共展示了thread.join方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_enumerate_after_join
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_enumerate_after_join(self):
# Try hard to trigger #1703448: a thread is still returned in
# threading.enumerate() after it has been join()ed.
enum = threading.enumerate
old_interval = sys.getcheckinterval()
try:
for i in xrange(1, 100):
# Try a couple times at each thread-switching interval
# to get more interleavings.
sys.setcheckinterval(i // 5)
t = threading.Thread(target=lambda: None)
t.start()
t.join()
l = enum()
self.assertNotIn(t, l,
"#1703448 triggered after %d trials: %s" % (i, l))
finally:
sys.setcheckinterval(old_interval)
示例2: test_is_alive_after_fork
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_is_alive_after_fork(self):
# Try hard to trigger #18418: is_alive() could sometimes be True on
# threads that vanished after a fork.
old_interval = sys.getcheckinterval()
# Make the bug more likely to manifest.
sys.setcheckinterval(10)
try:
for i in range(20):
t = threading.Thread(target=lambda: None)
t.start()
pid = os.fork()
if pid == 0:
os._exit(1 if t.is_alive() else 0)
else:
t.join()
pid, status = os.waitpid(pid, 0)
self.assertEqual(0, status)
finally:
sys.setcheckinterval(old_interval)
示例3: test_BoundedSemaphore_limit
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_BoundedSemaphore_limit(self):
# BoundedSemaphore should raise ValueError if released too often.
for limit in range(1, 10):
bs = threading.BoundedSemaphore(limit)
threads = [threading.Thread(target=bs.acquire)
for _ in range(limit)]
for t in threads:
t.start()
for t in threads:
t.join()
threads = [threading.Thread(target=bs.release)
for _ in range(limit)]
for t in threads:
t.start()
for t in threads:
t.join()
self.assertRaises(ValueError, bs.release)
示例4: test_3_join_in_forked_from_thread
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_3_join_in_forked_from_thread(self):
# Like the test above, but fork() was called from a worker thread
# In the forked process, the main Thread object must be marked as stopped.
script = """if 1:
main_thread = threading.current_thread()
def worker():
childpid = os.fork()
if childpid != 0:
os.waitpid(childpid, 0)
sys.exit(0)
t = threading.Thread(target=joiningfunc,
args=(main_thread,))
print 'end of main'
t.start()
t.join() # Should not block: main_thread is already stopped
w = threading.Thread(target=worker)
w.start()
"""
self._run_and_join(script)
示例5: test_reinit_tls_after_fork
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_reinit_tls_after_fork(self):
# Issue #13817: fork() would deadlock in a multithreaded program with
# the ad-hoc TLS implementation.
def do_fork_and_wait():
# just fork a child process and wait it
pid = os.fork()
if pid > 0:
os.waitpid(pid, 0)
else:
os._exit(0)
# start a bunch of threads that will fork() child processes
threads = []
for i in range(16):
t = threading.Thread(target=do_fork_and_wait)
threads.append(t)
t.start()
for t in threads:
t.join()
示例6: test_print_exception_stderr_is_none_2
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_print_exception_stderr_is_none_2(self):
script = r"""if 1:
import sys
import threading
import time
running = False
def run():
global running
running = True
while running:
time.sleep(0.01)
1.0/0.0
sys.stderr = None
t = threading.Thread(target=run)
t.start()
while not running:
time.sleep(0.01)
running = False
t.join()
"""
rc, out, err = assert_python_ok("-c", script)
self.assertEqual(out, '')
self.assertNotIn("Unhandled exception", err)
示例7: _run_and_join
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def _run_and_join(self, script):
script = """if 1:
import sys, os, time, threading
# a thread, which waits for the main program to terminate
def joiningfunc(mainthread):
mainthread.join()
print 'end of thread'
\n""" + script
p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE)
rc = p.wait()
data = p.stdout.read().replace('\r', '')
p.stdout.close()
self.assertEqual(data, "end of main\nend of thread\n")
self.assertFalse(rc == 2, "interpreter was blocked")
self.assertTrue(rc == 0, "Unexpected error")
示例8: test_print_exception_stderr_is_none_2
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def test_print_exception_stderr_is_none_2(self):
script = r"""if 1:
import sys
import threading
import time
running = False
def run():
global running
running = True
while running:
time.sleep(0.01)
1/0
sys.stderr = None
t = threading.Thread(target=run)
t.start()
while not running:
time.sleep(0.01)
running = False
t.join()
"""
rc, out, err = assert_python_ok("-c", script)
self.assertEqual(out, '')
self.assertNotIn("Unhandled exception", err)
示例9: background_command
# 需要导入模块: import thread [as 别名]
# 或者: from thread import join [as 别名]
def background_command(command, require_zero_status=False):
"""Executes a command in a separate thread, like running with '&' in the shell.
If you want the program to die if the command eventually returns with
nonzero status, then set require_zero_status to True. 'command' will be
executed in 'shell' mode, so it's OK for it to contain pipes and other
shell constructs.
This function returns the Thread object created, just in case you want
to wait for that specific command to finish. For example, you could do:
thread = background_command('foo | bar')
# do something else while waiting for it to finish
thread.join()
See also:
- wait_for_background_commands(), which can be used
at the end of the program to wait for all these commands to terminate.
- execute_command() and get_command_stdout(), which allow you to
execute commands in the foreground.
"""
p = subprocess.Popen(command, shell=True)
thread = threading.Thread(target=background_command_waiter, args=(command, p, require_zero_status))
thread.daemon = True # make sure it exits if main thread is terminated abnormally.
thread.start()
return thread