Python threading.active_count() Examples

The following are 30 code examples of threading.active_count(). 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 also want to check out all available functions/classes of the module threading , or try the search function .
Example #1
Source File: test_base.py    From rteeg with 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 #2
Source File: test_threading.py    From ironpython2 with 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 #3
Source File: test_threading.py    From BinderFilter with 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 #4
Source File: async.py    From cascade-server with 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 #5
Source File: test_threading.py    From oss-ftp with 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
Source File: test_process_worker.py    From testplan with 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
Source File: executor.py    From TimeMachine with 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
Source File: base_consumer.py    From distributed_framework with 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
Source File: custom_threadpool_executor.py    From distributed_framework with 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
Source File: test_threading.py    From Fluid-Designer with 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
Source File: test_threading.py    From ironpython3 with 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
Source File: test_multi.py    From execnet with 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
Source File: interface.py    From HRIM with 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 #14
Source File: spider.py    From Weblogic-Weakpassword-Scnner with 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 #15
Source File: sapphire_load_manager.py    From grizzly with 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 #16
Source File: sapphire_worker.py    From grizzly with 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 #17
Source File: pig_chase_baseline.py    From malmo-challenge with 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 #18
Source File: pig_chase_dqn.py    From malmo-challenge with 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
Source File: pig_chase_dqn_top_down.py    From malmo-challenge with 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
Source File: test_threading.py    From gcblue with 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 #21
Source File: test_multi.py    From execnet with 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 #22
Source File: demo.py    From twitterDataMining with 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
Source File: test_threading.py    From Project-New-Reign---Nemesis-Main with 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 #24
Source File: zmirror.py    From zmirror with 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 #25
Source File: zynthian_gui.py    From zynthian-ui with GNU General Public License v3.0 5 votes vote down vote up
def wait_threads_end(self, n=20):
		logging.debug("Awaiting threads to end ...")

		while (self.loading_thread.is_alive() or self.zyncoder_thread.is_alive() or zynautoconnect.is_running()) and n>0:
			sleep(0.1)
			n -= 1

		if n<=0:
			logging.error("Reached maximum count while awaiting threads to end!")
			return False
		else:
			logging.debug("Remaining {} active threads...".format(threading.active_count()))
			sleep(0.5)
			return True 
Example #26
Source File: TopicTrendsManager.py    From twitterDataMining with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        if self.param.mode != 2:  # online stream data(use twitter API)
            twitter_stream = TwitterStream(self.child_conn)
            twitter_stream_thread = threading.Thread(target=twitter_stream.stream_data,
                                                     args=(self.param.track, self.param.follow, self.param.location,
                                                           self.param.storeIntoDB, self.param.storeIntoDBName,))
            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.ready_receive()
                tweets = self.parent_conn.recv()
                t = threading.Thread(target=self.do_some_from_data, args=(tweets,))
                t.setDaemon(True)
                t.start()

        else:  # local database data
            condition = threading.Condition()
            local_stream = LocalStream()
            local_stream_thread = threading.Thread(target=local_stream.stream_data,
                                                   args=(condition, self.param.startDate, self.param.endDate,
                                                         self.param.localCollectionsName,))
            local_stream_thread.setDaemon(True)
            local_stream_thread.start()
            print ' threading.active_count()', threading.active_count()

            if condition.acquire():
                while True:
                    print 'wait to receive'
                    if local_stream.tweets:
                        self.do_some_from_data(local_stream.tweets)
                        local_stream.tweets = []
                        condition.notify()

                    condition.wait() 
Example #27
Source File: conftest.py    From moler with 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
Source File: asyncio_runner.py    From moler with 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
Source File: honeypot.py    From yalih with Apache License 2.0 5 votes vote down vote up
def threadmaker():
	
	while True:
		
		threadstomake = honeypotconfig.threadnum - threading.active_count()
		
		for i in range(threadstomake):
			thread = threading.Thread(target=worker)
			thread.setDaemon(True)
		 	thread.start()

		time.sleep(5) 
Example #30
Source File: quit.py    From PyIris-backdoor with Mozilla Public License 2.0 5 votes vote down vote up
def main():
    try:
        confirm_exit = input('\n' + config.pro + 'Are you sure you want to exit[y/n] : ')
        if confirm_exit == 'y':
            print(config.inf + 'User requested shutdown...')
            if config.listener_database:
                print(config.lod + 'Killing all active listeners')
                print(config.inf + 'Sent kill message to all listeners...')
                print(config.inf + 'Waiting for response...')
                config.listener_database = {}
                while threading.active_count() > 1:
                    continue
                print(config.pos + 'Done')
            if config.scout_database:
                print(config.lod + 'Disconnecting all scouts')
                for i in config.scout_database:
                    try:
                        send_all.main(config.scout_database[i][0], 'disconnect')
                        config.scout_database[i][0].settimeout(5)
                        buffer_out_reply = recv_all.main(config.scout_database[i][0])
                        config.scout_database[i][0].close()
                        print(config.pos + 'Closed connection to scout of ID : ' + i)
                    except (socket.error, socket.timeout):
                        print(config.neg + 'Could not close connection to scout of ID : ' + i)
                        pass
                print(config.pos + 'Done')
            print(config.pos + 'Exiting...')
            os._exit(1)
        else:
            pass
    except EOFError:
        try:
            time.sleep(2)
            quit()
        except KeyboardInterrupt:
            quit()
    except KeyboardInterrupt:
        quit()