def access_register(proxy=None, timeout_check=True):
i = 0
while i < _max_register_count:
try:
if timeout_check:
TimeoutTracker(try_register)(proxy=proxy)
else:
print(try_register(proxy=proxy))
i += 1
print("SUCCESS %d" % i)
except Exception as e:
print(str(e) + 'Error , retrying')
finally:
for p in multiprocessing.active_children():
p.terminate()
gc.collect()
python类active_children()的实例源码
def test_terminate(self):
if self.TYPE == 'threads':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
p = self.Process(target=self._test_terminate)
p.daemon = True
p.start()
self.assertEqual(p.is_alive(), True)
self.assertIn(p, self.active_children())
self.assertEqual(p.exitcode, None)
p.terminate()
join = TimingWrapper(p.join)
self.assertEqual(join(), None)
self.assertTimingAlmostEqual(join.elapsed, 0.0)
self.assertEqual(p.is_alive(), False)
self.assertNotIn(p, self.active_children())
p.join()
# XXX sometimes get p.exitcode == 0 on Windows ...
#self.assertEqual(p.exitcode, -signal.SIGTERM)
def test_multiprocessing():
"""Tests that the number of children we produce is correct"""
# Selects a number at random so we can spot check
num_workers = random.choice(range(2, multiprocessing.cpu_count() * 2 + 1))
app = Sanic('test_multiprocessing')
process_list = set()
def stop_on_alarm(*args):
for process in multiprocessing.active_children():
process_list.add(process.pid)
process.terminate()
signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1)
app.run(HOST, PORT, workers=num_workers)
assert len(process_list) == num_workers
def tearDownClass(cls):
# only the manager process should be returned by active_children()
# but this can take a bit on slow machines, so wait a few seconds
# if there are other children too (see #17395)
t = 0.01
while len(multiprocessing.active_children()) > 1 and t < 5:
time.sleep(t)
t *= 2
gc.collect() # do garbage collection
if cls.manager._number_of_objects() != 0:
# This is not really an error since some tests do not
# ensure that all processes which hold a reference to a
# managed object have been joined.
print('Shared objects which still exist at manager shutdown:')
print(cls.manager._debug_info())
cls.manager.shutdown()
cls.manager.join()
cls.manager = None
def run_pool(workers, n_jobs=-1, sleep=0.1):
# defensive copy
workers = workers[:]
if n_jobs < 1:
n_jobs = multiprocessing.cpu_count()
processes = []
p = None
try:
while True:
active = multiprocessing.active_children()
while len(active) < n_jobs and len(workers) > 0:
p = workers.pop(0)
p.start()
processes.append(p)
active = multiprocessing.active_children()
if len(workers) == 0 and len(active) == 0:
break
time.sleep(sleep)
except (KeyboardInterrupt, SystemExit):
if p is not None:
p.terminate()
for p in processes:
p.terminate()
raise
def scanning_boosters(self):
proces=[]
for ip in self.target:
k=len(multiprocessing.active_children())
if k==self.thread:
time.sleep(3)
self.thread=self.thread+30
mythread=multiprocessing.Process(target=self.checkping, args=(ip,))
mythread.start()
proces.append(mythread)
for mythread in proces:
mythread.join()
self.timeclose=time.time()
self.showing_results()
return
# Printing Function
def test_multiprocessing():
"""Tests that the number of children we produce is correct"""
# Selects a number at random so we can spot check
num_workers = random.choice(range(2, multiprocessing.cpu_count() * 2 + 1))
app = Mach9('test_multiprocessing')
process_list = set()
def stop_on_alarm(*args):
for process in multiprocessing.active_children():
process_list.add(process.pid)
process.terminate()
signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1)
app.run(HOST, PORT, workers=num_workers)
assert len(process_list) == num_workers
def producer(self, producer_instance):
with producer_instance as producer:
yield producer
assert len(multiprocessing.active_children()) == 0
def test_messages_not_duplicated(self, message, producer_instance):
with capture_new_messages(
message.topic
) as get_messages, producer_instance as producer:
producer.publish(message)
producer.flush()
assert len(multiprocessing.active_children()) == 0
assert len(get_messages()) == 1
def test_child_processes_do_not_survive_an_exception(self, producer_instance, message):
with pytest.raises(RandomException), producer_instance as producer:
producer.publish(message)
producer.flush()
producer.publish(message)
raise RandomException()
assert len(multiprocessing.active_children()) == 0
def test_skip_publish_pii_message(self, pii_schema, payload, producer_instance):
with reconfigure(
encryption_type='AES_MODE_CBC-1',
skip_messages_with_pii=True
), producer_instance as producer, mock.patch.object(
data_pipeline._kafka_producer,
'logger'
) as mock_logger:
pii_message = CreateMessage(
schema_id=pii_schema.schema_id,
payload=payload
)
messages = self._publish_message(pii_message, producer)
assert len(messages) == 0
assert len(multiprocessing.active_children()) == 0
call_args = (
"Skipping a PII message - uuid hex: {}, schema_id: {}, "
"timestamp: {}, type: {}"
).format(
pii_message.uuid_hex,
pii_message.schema_id,
pii_message.timestamp,
pii_message.message_type.name
)
assert mock_logger.info.call_args_list[0] == mock.call(call_args)
def test_publish_pii_payload_data_message(
self, pii_schema, example_payload_data, producer_instance
):
with reconfigure(
encryption_type='AES_MODE_CBC-1',
skip_messages_with_pii=False
), producer_instance as producer:
pii_message = CreateMessage(
schema_id=pii_schema.schema_id,
payload_data=example_payload_data
)
self._publish_and_assert_pii_message(pii_message, producer)
assert len(multiprocessing.active_children()) == 0
def shutdown(self, c):
'''
Shutdown this process
'''
try:
try:
util.debug('manager received shutdown message')
c.send(('#RETURN', None))
if sys.stdout != sys.__stdout__:
util.debug('resetting stdout, stderr')
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
util._run_finalizers(0)
for p in active_children():
util.debug('terminating a child process of manager')
p.terminate()
for p in active_children():
util.debug('terminating a child process of manager')
p.join()
util._run_finalizers()
util.info('manager exiting with exitcode 0')
except:
import traceback
traceback.print_exc()
finally:
exit(0)
def _wait_for_processors_to_free_up(self, max_concurrent_processes):
while len(active_children()) >= max_concurrent_processes:
self.logger.debug("Waiting a few seconds for processors to free up")
time.sleep(0.1)
def kill_process(self):
proc_list = []
while multiprocessing.active_children():
for p in multiprocessing.active_children():
if p.is_alive():
p.terminate()
proc_list.append(p.pid)
if self.is_alive():
self.terminate()
proc_list.append(self.pid)
print ("\nTerminated Autorepeat Processes:", *set(proc_list), sep=' ')
def kill_children():
"""
Kill child process
"""
for proc in multiprocessing.active_children():
LOG.info('Terminating %r [%d] ...', proc, proc.pid)
proc.terminate()
parent = psutil.Process(os.getpid())
for child in parent.children(recursive=True):
LOG.info('Terminating process %r', child)
child.kill()
def test_active_children(self):
self.assertEqual(type(self.active_children()), list)
p = self.Process(target=time.sleep, args=(DELTA,))
self.assertNotIn(p, self.active_children())
p.daemon = True
p.start()
self.assertIn(p, self.active_children())
p.join()
self.assertNotIn(p, self.active_children())
def test_number_of_objects(self):
EXPECTED_NUMBER = 1 # the pool object is still alive
multiprocessing.active_children() # discard dead process objs
gc.collect() # do garbage collection
refs = self.manager._number_of_objects()
debug_info = self.manager._debug_info()
if refs != EXPECTED_NUMBER:
print self.manager._debug_info()
print debug_info
self.assertEqual(refs, EXPECTED_NUMBER)
#
# Test of creating a customized manager class
#
def test_process(self):
q = self.Queue(1)
e = self.Event()
args = (q, 1, 2)
kwargs = {'hello':23, 'bye':2.54}
name = 'SomeProcess'
p = self.Process(
target=self._test, args=args, kwargs=kwargs, name=name
)
p.daemon = True
current = self.current_process()
if self.TYPE != 'threads':
self.assertEqual(p.authkey, current.authkey)
self.assertEqual(p.is_alive(), False)
self.assertEqual(p.daemon, True)
self.assertNotIn(p, self.active_children())
self.assertTrue(type(self.active_children()) is list)
self.assertEqual(p.exitcode, None)
p.start()
self.assertEqual(p.exitcode, None)
self.assertEqual(p.is_alive(), True)
self.assertIn(p, self.active_children())
self.assertEqual(q.get(), args[1:])
self.assertEqual(q.get(), kwargs)
self.assertEqual(q.get(), p.name)
if self.TYPE != 'threads':
self.assertEqual(q.get(), current.authkey)
self.assertEqual(q.get(), p.pid)
p.join()
self.assertEqual(p.exitcode, 0)
self.assertEqual(p.is_alive(), False)
self.assertNotIn(p, self.active_children())
def test_terminate(self):
if self.TYPE == 'threads':
self.skipTest('test not appropriate for {}'.format(self.TYPE))
p = self.Process(target=self._test_terminate)
p.daemon = True
p.start()
self.assertEqual(p.is_alive(), True)
self.assertIn(p, self.active_children())
self.assertEqual(p.exitcode, None)
p.terminate()
join = TimingWrapper(p.join)
self.assertEqual(join(), None)
self.assertTimingAlmostEqual(join.elapsed, 0.0)
self.assertEqual(p.is_alive(), False)
self.assertNotIn(p, self.active_children())
p.join()
# XXX sometimes get p.exitcode == 0 on Windows ...
#self.assertEqual(p.exitcode, -signal.SIGTERM)
def test_active_children(self):
self.assertEqual(type(self.active_children()), list)
p = self.Process(target=time.sleep, args=(DELTA,))
self.assertNotIn(p, self.active_children())
p.daemon = True
p.start()
self.assertIn(p, self.active_children())
p.join()
self.assertNotIn(p, self.active_children())
def test_number_of_objects(self):
EXPECTED_NUMBER = 1 # the pool object is still alive
multiprocessing.active_children() # discard dead process objs
gc.collect() # do garbage collection
refs = self.manager._number_of_objects()
debug_info = self.manager._debug_info()
if refs != EXPECTED_NUMBER:
print self.manager._debug_info()
print debug_info
self.assertEqual(refs, EXPECTED_NUMBER)
#
# Test of creating a customized manager class
#
def shutdown(self, c):
'''
Shutdown this process
'''
try:
try:
util.debug('manager received shutdown message')
c.send(('#RETURN', None))
if sys.stdout != sys.__stdout__:
util.debug('resetting stdout, stderr')
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
util._run_finalizers(0)
for p in active_children():
util.debug('terminating a child process of manager')
p.terminate()
for p in active_children():
util.debug('terminating a child process of manager')
p.join()
util._run_finalizers()
util.info('manager exiting with exitcode 0')
except:
import traceback
traceback.print_exc()
finally:
exit(0)
def on_shutdown(self):
for child in multiprocessing.active_children():
self._logger.info("Killed Child")
child.terminate()
self._logger.info("Meta Reader Terminated")
def test_process(self):
q = self.Queue(1)
e = self.Event()
args = (q, 1, 2)
kwargs = {'hello':23, 'bye':2.54}
name = 'SomeProcess'
p = self.Process(
target=self._test, args=args, kwargs=kwargs, name=name
)
p.daemon = True
current = self.current_process()
if self.TYPE != 'threads':
self.assertEqual(p.authkey, current.authkey)
self.assertEqual(p.is_alive(), False)
self.assertEqual(p.daemon, True)
self.assertNotIn(p, self.active_children())
self.assertTrue(type(self.active_children()) is list)
self.assertEqual(p.exitcode, None)
p.start()
self.assertEqual(p.exitcode, None)
self.assertEqual(p.is_alive(), True)
self.assertIn(p, self.active_children())
self.assertEqual(q.get(), args[1:])
self.assertEqual(q.get(), kwargs)
self.assertEqual(q.get(), p.name)
if self.TYPE != 'threads':
self.assertEqual(q.get(), current.authkey)
self.assertEqual(q.get(), p.pid)
p.join()
self.assertEqual(p.exitcode, 0)
self.assertEqual(p.is_alive(), False)
self.assertNotIn(p, self.active_children())
def test_active_children(self):
self.assertEqual(type(self.active_children()), list)
p = self.Process(target=time.sleep, args=(DELTA,))
self.assertNotIn(p, self.active_children())
p.daemon = True
p.start()
self.assertIn(p, self.active_children())
p.join()
self.assertNotIn(p, self.active_children())
def shutdown(self, c):
'''
Shutdown this process
'''
try:
try:
util.debug('manager received shutdown message')
c.send(('#RETURN', None))
if sys.stdout != sys.__stdout__:
util.debug('resetting stdout, stderr')
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
util._run_finalizers(0)
for p in active_children():
util.debug('terminating a child process of manager')
p.terminate()
for p in active_children():
util.debug('terminating a child process of manager')
p.join()
util._run_finalizers()
util.info('manager exiting with exitcode 0')
except:
import traceback
traceback.print_exc()
finally:
exit(0)
def _map_len(self):
return len(multiprocessing.active_children())
def shutdown(self, c):
'''
Shutdown this process
'''
try:
try:
util.debug('manager received shutdown message')
c.send(('#RETURN', None))
if sys.stdout != sys.__stdout__:
util.debug('resetting stdout, stderr')
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
util._run_finalizers(0)
for p in active_children():
util.debug('terminating a child process of manager')
p.terminate()
for p in active_children():
util.debug('terminating a child process of manager')
p.join()
util._run_finalizers()
util.info('manager exiting with exitcode 0')
except:
import traceback
traceback.print_exc()
finally:
exit(0)
def onClosing(self):
if messagebox.askokcancel("Quit","do you want to Quit?"):
for child in multiprocessing.active_children():
kill_proc_tree(child.pid)
if self.running:
killFCEUX()
self.master.destroy()
self.master.quit()