当前位置: 首页>>代码示例>>Python>>正文


Python thread.start方法代码示例

本文整理汇总了Python中thread.start方法的典型用法代码示例。如果您正苦于以下问题:Python thread.start方法的具体用法?Python thread.start怎么用?Python thread.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在thread的用法示例。


在下文中一共展示了thread.start方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_join_nondaemon_on_shutdown

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [as 别名]
def test_join_nondaemon_on_shutdown(self):
        # Issue 1722344
        # Raising SystemExit skipped threading._shutdown
        p = subprocess.Popen([sys.executable, "-c", """if 1:
                import threading
                from time import sleep

                def child():
                    sleep(1)
                    # As a non-daemon thread we SHOULD wake up and nothing
                    # should be torn down yet
                    print "Woke up, sleep function is:", sleep

                threading.Thread(target=child).start()
                raise SystemExit
            """],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stderr.close)
        stdout, stderr = p.communicate()
        self.assertEqual(stdout.strip(),
            "Woke up, sleep function is: <built-in function sleep>")
        stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
        self.assertEqual(stderr, "") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test_threading.py

示例2: test_enumerate_after_join

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:20,代码来源:test_threading.py

示例3: test_is_alive_after_fork

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:23,代码来源:test_threading.py

示例4: test_BoundedSemaphore_limit

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:19,代码来源:test_threading.py

示例5: test_3_join_in_forked_from_thread

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:23,代码来源:test_threading.py

示例6: test_reinit_tls_after_fork

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:23,代码来源:test_threading.py

示例7: test_print_exception

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [as 别名]
def test_print_exception(self):
        script = r"""if 1:
            import threading
            import time

            running = False
            def run():
                global running
                running = True
                while running:
                    time.sleep(0.01)
                1.0/0.0
            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.assertIn("Exception in thread", err)
        self.assertIn("Traceback (most recent call last):", err)
        self.assertIn("ZeroDivisionError", err)
        self.assertNotIn("Unhandled exception", err) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test_threading.py

示例8: test_print_exception

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [as 别名]
def test_print_exception(self):
        script = r"""if 1:
            import threading
            import time

            running = False
            def run():
                global running
                running = True
                while running:
                    time.sleep(0.01)
                1/0
            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.assertIn("Exception in thread", err)
        self.assertIn("Traceback (most recent call last):", err)
        self.assertIn("ZeroDivisionError", err)
        self.assertNotIn("Unhandled exception", err) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:27,代码来源:test_threading.py

示例9: background_command

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [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 
开发者ID:hsn-zeinali,项目名称:x-vector-kaldi-tf,代码行数:27,代码来源:ze_utils.py

示例10: test_join_nondaemon_on_shutdown

# 需要导入模块: import thread [as 别名]
# 或者: from thread import start [as 别名]
def test_join_nondaemon_on_shutdown(self):
        # Issue 1722344
        # Raising SystemExit skipped threading._shutdown
        p = subprocess.Popen([sys.executable, "-c", """if 1:
                import threading
                from time import sleep

                def child():
                    sleep(1)
                    # As a non-daemon thread we SHOULD wake up and nothing
                    # should be torn down yet
                    print "Woke up, sleep function is:", sleep

                threading.Thread(target=child).start()
                raise SystemExit
            """],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        self.addCleanup(p.stdout.close)
        self.addCleanup(p.stderr.close)
        stdout, stderr = p.communicate()
        self.assertTrue(stdout.strip().startswith(
            "Woke up, sleep function is: <java function sleep"))
        stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
        self.assertEqual(stderr, "") 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:27,代码来源:test_threading.py


注:本文中的thread.start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。