Python threading.active_count() Examples

The following are 30 code examples for showing how to use threading.active_count(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module threading , or try the search function .

Example 1
Project: zmirror   Author: aploium   File: zmirror.py    License: MIT License 6 votes vote down vote up
def zmirror_status():
    """返回服务器的一些状态信息"""
    if request.remote_addr and request.remote_addr != '127.0.0.1':
        return generate_simple_resp_page(b'Only 127.0.0.1 are allowed', 403)
    output = ""
    output += strx('extract_real_url_from_embedded_url', extract_real_url_from_embedded_url.cache_info())
    output += strx('\nis_content_type_streamed', is_mime_streamed.cache_info())
    output += strx('\nembed_real_url_to_embedded_url', embed_real_url_to_embedded_url.cache_info())
    output += strx('\ncheck_global_ua_pass', check_global_ua_pass.cache_info())
    output += strx('\nextract_mime_from_content_type', extract_mime_from_content_type.cache_info())
    output += strx('\nis_content_type_using_cdn', is_content_type_using_cdn.cache_info())
    output += strx('\nis_ua_in_whitelist', is_content_type_using_cdn.cache_info())
    output += strx('\nis_mime_represents_text', is_mime_represents_text.cache_info())
    output += strx('\nis_domain_match_glob_whitelist', is_domain_match_glob_whitelist.cache_info())
    output += strx('\nverify_ip_hash_cookie', verify_ip_hash_cookie.cache_info())
    output += strx('\nis_denied_because_of_spider', is_denied_because_of_spider.cache_info())
    output += strx('\nis_ip_not_in_allow_range', is_ip_not_in_allow_range.cache_info())
    output += strx('\n\ncurrent_threads_number', threading.active_count())
    # output += strx('\nclient_requests_text_rewrite', client_requests_text_rewrite.cache_info())
    # output += strx('\nextract_url_path_and_query', extract_url_path_and_query.cache_info())

    output += strx('\n----------------\n')
    output += strx('\ndomain_alias_to_target_set', domain_alias_to_target_set)

    return "<pre>" + output + "</pre>\n" 
Example 2
Project: cascade-server   Author: mitre   File: async.py    License: Apache License 2.0 6 votes vote down vote up
def enable_async():
    global enabled

    if enabled:
        return enabled

    if threading.active_count() > 3:
        # This number used to be 1, but a gevent patch or something else changed this so it starts with 3 threads
        logger.warning('{} threads already running. gvent monkey patching disabled...'.format(threading.active_count()))
        enabled = False

    else:
        logger.debug('Monkey patching using gevent')
        gevent.monkey.patch_all()
        enabled = True

    return enabled 
Example 3
Project: ironpython2   Author: IronLanguages   File: test_threading.py    License: Apache License 2.0 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, '')
        self.assertEqual(err, '') 
Example 4
Project: BinderFilter   Author: dxwu   File: test_threading.py    License: MIT License 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, '')
        self.assertEqual(err, '') 
Example 5
Project: oss-ftp   Author: aliyun   File: test_threading.py    License: MIT License 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, '')
        self.assertEqual(err, '') 
Example 6
Project: testplan   Author: Morgan-Stanley   File: test_process_worker.py    License: Apache License 2.0 6 votes vote down vote up
def test_start_stop(self, proc_pool):
        """Test basic start/stop of ProcessPool."""
        # This testcase is known to fail on Windows - mark as xfail until we
        # can fix it up.
        if platform.system() == "Windows":
            pytest.xfail("ProcPool start/stop is unstable on Windows")

        current_proc = psutil.Process()
        start_children = current_proc.children()
        start_thread_count = threading.active_count()

        # Iterate 5 times to increase the chance of hitting a race condition.
        for _ in range(5):
            with proc_pool:
                assert proc_pool.status.tag == proc_pool.status.STARTED
                assert len(current_proc.children()) == len(start_children) + 2

            assert proc_pool.status.tag == proc_pool.status.STOPPED
            assert len(current_proc.children()) == len(start_children) 
Example 7
Project: TimeMachine   Author: DroidTest   File: executor.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def output(self):
        
        with open(RunParameters.OUTPUT_FILE, "a") as csv_file:
            writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            while (time.time() - self.start_time) < self.time_limit:
                
                #computing snapshots
                num_snapshots = 0
                for key in self.state_graph.states:
                    if self.state_graph.states[key].solid:
                        num_snapshots = num_snapshots+1
                
                #read coverage
                coverage_manager.pull_coverage_files("temp")
                coverage_manager.compute_current_coverage()             # output in coverage.txt
                current_coverage = coverage_manager.read_current_coverage()

                # write files
                writer.writerow([str(int(time.time()-self.start_time)), str(len(self.state_graph.states)),str(num_snapshots), str(self.num_restore), str(current_coverage)])
                time.sleep(120)
                
                print "current threads:  " + str(threading.active_count())


        csv_file.close() 
Example 8
Project: distributed_framework   Author: ydf0509   File: base_consumer.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, queue_name, fucntion_name, params):
        self.queue_name = queue_name
        self.function = fucntion_name
        publish_time = _get_publish_time(params)
        if publish_time:
            self.publish_time_str = time_util.DatetimeConverter(publish_time).datetime_str
        function_params = _delete_keys_and_return_new_dict(params, )
        self.params = function_params
        self.params_str = json.dumps(function_params, ensure_ascii=False)
        self.result = ''
        self.run_times = 0
        self.exception = ''
        self.time_start = time.time()
        self.time_cost = None
        self.success = False
        self.current_thread = ConsumersManager.get_concurrent_info()
        self.total_thread = threading.active_count()
        self.set_log_level(20) 
Example 9
Project: distributed_framework   Author: ydf0509   File: custom_threadpool_executor.py    License: Apache License 2.0 6 votes vote down vote up
def show_current_threads_num(sleep_time=60, process_name='', block=False):
    process_name = sys.argv[0] if process_name == '' else process_name

    def _show_current_threads_num():
        while True:
            # logger_show_current_threads_num.info(f'{process_name} 进程 的 并发数量是 -->  {threading.active_count()}')
            nb_print(f'{process_name} 进程 的 线程数量是 -->  {threading.active_count()}')
            time.sleep(sleep_time)

    if process_name not in process_name_set:
        if block:
            _show_current_threads_num()
        else:
            t = threading.Thread(target=_show_current_threads_num, daemon=True)
            t.start()
        process_name_set.add(process_name) 
Example 10
Project: Fluid-Designer   Author: Microvellum   File: test_threading.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import _thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            _thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, b'')
        self.assertEqual(err, b'') 
Example 11
Project: ironpython3   Author: IronLanguages   File: test_threading.py    License: Apache License 2.0 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import _thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            _thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, b'')
        self.assertEqual(err, b'') 
Example 12
Project: execnet   Author: pytest-dev   File: test_multi.py    License: MIT License 6 votes vote down vote up
def test_safe_terminate(execmodel):
    if execmodel.backend != "threading":
        pytest.xfail(
            "execution model %r does not support task count" % execmodel.backend
        )
    import threading

    active = threading.active_count()
    l = []

    def term():
        sleep(3)

    def kill():
        l.append(1)

    safe_terminate(execmodel, 1, [(term, kill)] * 10)
    assert len(l) == 10
    sleep(0.1)
    gc.collect()
    assert execmodel.active_count() == active 
Example 13
Project: execnet   Author: pytest-dev   File: test_multi.py    License: MIT License 6 votes vote down vote up
def test_safe_terminate2(execmodel):
    if execmodel.backend != "threading":
        pytest.xfail(
            "execution model %r does not support task count" % execmodel.backend
        )
    import threading

    active = threading.active_count()
    l = []

    def term():
        return

    def kill():
        l.append(1)

    safe_terminate(execmodel, 3, [(term, kill)] * 10)
    assert len(l) == 0
    sleep(0.1)
    gc.collect()
    assert threading.active_count() == active 
Example 14
Project: HRIM   Author: AcutronicRobotics   File: interface.py    License: Apache License 2.0 6 votes vote down vote up
def nodeSelected(self, value):
        try:

            self.validNode = False
            counter = 0
            while(threading.active_count()>2 and counter<100):
                time.sleep(0.01)

            self.resetAll()

            if value>0 and counter<100:
                self.node_name = self.node_list[value-1]
                if "/"+self.node_name+"/specs" in dict(self.node.get_service_names_and_types()):
                    self.validNode = True
                    self.connectToNode()
                else:
                    self.validNode = False
                    print("Not a valid HRIM gripper node")
        except:
            raise 
Example 15
Project: Weblogic-Weakpassword-Scnner   Author: dc3l1ne   File: spider.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _main(self):
		with open(sys.argv[1]) as f:
			self.total=sum(bl.count("\n") for bl in self.blocks(f))
			print self.total
		t = threading.Thread(target=self.read_url_list)
		t.daemon = True
		t.start()
		time.sleep(5)
		for i in range(0,MAX_THREAD):
			t = threading.Thread(target=self._check)
			t.daemon = True
			t.start()
		while self.url_queue.qsize() !=0:
			sys.stdout.write("Current threads: %d,URLs left: %d,Success:%d                             \r" % (
			threading.active_count(), self.total, self.success), )
			sys.stdout.flush()
			time.sleep(0.3)
		while threading.active_count() > 1:
			sys.stdout.write("Current threads: %d,URLs left: %d,Success:%d                             \r" % (
			threading.active_count(), self.total, self.success), )
			sys.stdout.flush()
			time.sleep(1) 
Example 16
Project: grizzly   Author: MozillaSecurity   File: sapphire_load_manager.py    License: Mozilla Public License 2.0 6 votes vote down vote up
def start(self):
        assert self._job.pending
        # create the listener thread to handle incoming requests
        listener = threading.Thread(
            target=self.listener,
            args=(self._socket, self._job, self._workers),
            kwargs={"shutdown_delay": self.SHUTDOWN_DELAY})
        # launch listener thread and handle thread errors
        for retry in reversed(range(10)):
            try:
                listener.start()
            except threading.ThreadError:
                # thread errors can be due to low system resources while fuzzing
                LOG.warning("ThreadError (listener), threads: %d", threading.active_count())
                if retry < 1:
                    raise
                time.sleep(1)
                continue
            self._listener = listener
            break 
Example 17
Project: grizzly   Author: MozillaSecurity   File: sapphire_worker.py    License: Mozilla Public License 2.0 6 votes vote down vote up
def launch(cls, listen_sock, job):
        assert job.accepting.is_set()
        conn = None
        try:
            conn, _ = listen_sock.accept()
            conn.settimeout(None)
            # create a worker thread to handle client request
            w_thread = threading.Thread(target=cls.handle_request, args=(conn, job))
            job.accepting.clear()
            w_thread.start()
            return cls(conn, w_thread)
        except (socket.error, socket.timeout):
            if conn is not None:  # pragma: no cover
                conn.close()
        except threading.ThreadError:
            if conn is not None:  # pragma: no cover
                conn.close()
            # reset accepting status
            job.accepting.set()
            LOG.warning("ThreadError (worker), threads: %d", threading.active_count())
            # wait for system resources to free up
            time.sleep(0.1)
        return None 
Example 18
Project: malmo-challenge   Author: microsoft   File: pig_chase_baseline.py    License: MIT License 6 votes vote down vote up
def run_experiment(agents_def):
    assert len(agents_def) == 2, 'Not enough agents (required: 2, got: %d)'\
                % len(agents_def)

    processes = []
    for agent in agents_def:
        p = Thread(target=agent_factory, kwargs=agent)
        p.daemon = True
        p.start()

        # Give the server time to start
        if agent['role'] == 0:
            sleep(1)

        processes.append(p)

    try:
        # wait until only the challenge agent is left
        while active_count() > 2:
            sleep(0.1)
    except KeyboardInterrupt:
        print('Caught control-c - shutting down.') 
Example 19
Project: malmo-challenge   Author: microsoft   File: pig_chase_dqn.py    License: MIT License 6 votes vote down vote up
def run_experiment(agents_def):
    assert len(agents_def) == 2, 'Not enough agents (required: 2, got: %d)' \
                                 % len(agents_def)

    processes = []
    for agent in agents_def:
        p = Thread(target=agent_factory, kwargs=agent)
        p.daemon = True
        p.start()

        # Give the server time to start
        if agent['role'] == 0:
            sleep(1)

        processes.append(p)

    try:
        # wait until only the challenge agent is left
        while active_count() > 2:
            sleep(0.1)
    except KeyboardInterrupt:
        print('Caught control-c - shutting down.') 
Example 20
Project: malmo-challenge   Author: microsoft   File: pig_chase_dqn_top_down.py    License: MIT License 6 votes vote down vote up
def run_experiment(agents_def):
    assert len(agents_def) == 2, 'Not enough agents (required: 2, got: %d)' \
                                 % len(agents_def)

    processes = []
    for agent in agents_def:
        p = Thread(target=agent_factory, kwargs=agent)
        p.daemon = True
        p.start()

        # Give the server time to start
        if agent['role'] == 0:
            sleep(1)

        processes.append(p)

    try:
        # wait until only the challenge agent is left
        while active_count() > 2:
            sleep(0.1)
    except KeyboardInterrupt:
        print('Caught control-c - shutting down.') 
Example 21
Project: gcblue   Author: gcblue   File: test_threading.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, '')
        self.assertEqual(err, '') 
Example 22
Project: twitterDataMining   Author: hrwhisper   File: demo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def run(self):

        twitter_stream = TwitterStream(self.child_conn)
        twitter_stream_thread = threading.Thread(target=twitter_stream.run)
        twitter_stream_thread.setDaemon(True)
        twitter_stream_thread.start()

        print ' threading.active_count()', threading.active_count()
        # TODO error count > 3 kill
        while True:
            time.sleep(self.period)
            twitter_stream.get()
            tweets = self.parent_conn.recv()
            t = threading.Thread(target=self.do_some_from_data, args=(tweets,))
            t.setDaemon(True)
            t.start()
            print 'TopicTrends threading.live : ', list(threading.enumerate())

            # print sum(tweets)
        print 'end' 
Example 23
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import _thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            _thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, b'')
        self.assertEqual(err, b'') 
Example 24
Project: rteeg   Author: kaczmarj   File: test_base.py    License: MIT License 6 votes vote down vote up
def test_BaseStream_connect():
    event = threading.Event()
    def dummy_func():
        while not event.is_set():
            time.sleep(1.)
    base = BaseStream()
    n_threads_0 = threading.active_count()
    base.connect(dummy_func, "TEST")
    n_threads_1 = threading.active_count()
    # Check that a thread was started.
    assert n_threads_1 - n_threads_0 == 1, "Thread not started."

    # Check that the thread was created and named properly.
    name = [t.getName() for t in threading.enumerate() if t.getName() == "TEST"]
    assert name[0] == "TEST", "Thread not named properly."

    # Check that connect method only allows one connection.
    with pytest.raises(RuntimeError):
        base.connect(dummy_func, "SECOND_TEST")

    # Clean up.
    event.set() 
Example 25
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: totalThreads.py    License: MIT License 5 votes vote down vote up
def main():
  for i in range(random.randint(2,50)):
    thread = threading.Thread(target=myThread, args=(i,))
    thread.start()

  time.sleep(4)
  print("Total Number of Active Threads: {}".format(threading.active_count())) 
Example 26
Project: unicorn-binance-websocket-api   Author: oliver-zehentleitner   File: unicorn_binance_websocket_api_manager.py    License: MIT License 5 votes vote down vote up
def get_process_usage_threads(self):
        """
        Get the amount of threads that this process is using

        :return: int
        """
        threads = threading.active_count()
        return threads 
Example 27
Project: moler   Author: nokia   File: asyncio_runner.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def system_resources_usage():
    curr_fds_open = current_process.num_fds()
    curr_threads_nb = threading.active_count()
    return curr_fds_open, curr_threads_nb 
Example 28
Project: moler   Author: nokia   File: conftest.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def system_resources_usage():
    curr_fds_open = current_process.num_fds()
    curr_threads_nb = threading.active_count()
    return curr_fds_open, curr_threads_nb 
Example 29
Project: python-slackclient   Author: slackapi   File: test_issue_480.py    License: MIT License 5 votes vote down vote up
def test_issue_480_threads(self):
        client = self.sync_client
        before = threading.active_count()
        for idx in range(10):
            response = client.api_test()
            self.assertIsNotNone(response)
        after = threading.active_count()
        self.assertEqual(0, after - before)

    # fails with Python 3.6 
Example 30
Project: python-slackclient   Author: slackapi   File: test_issue_480.py    License: MIT License 5 votes vote down vote up
def test_issue_480_threads_async(self):
        client = self.async_client
        before = threading.active_count()
        for idx in range(10):
            response = await client.api_test()
            self.assertIsNotNone(response)
        after = threading.active_count()
        self.assertEqual(0, after - before)