def main_loop(config): global run_main_loop logger.info( u'Run main loop. Worker pool size={}. Sleep time is {}.'.format( config.WORKER_POOL_SIZE, config.SLEEP)) parent_pid = os.getpid() while True: if check_network_status(config.CHECK_URL, config.HTTP_TIMEOUT): required_workers_count = config.WORKER_POOL_SIZE - len( active_children()) if required_workers_count > 0: logger.info( 'Spawning {} workers'.format(required_workers_count)) spawn_workers(num=required_workers_count, target=worker, args=(config, ), parent_pid=parent_pid) else: logger.critical('Network is down. stopping workers') for c in active_children(): c.terminate() sleep(config.SLEEP) if break_func_for_test(): break
def main_loop(config): logger.info( u'Run main loop. Worker pool size={}. Sleep time is {}.'.format( config.WORKER_POOL_SIZE, config.SLEEP )) parent_pid = os.getpid() while True: if check_network_status(config.CHECK_URL, config.HTTP_TIMEOUT): required_workers_count = config.WORKER_POOL_SIZE - len( active_children()) if required_workers_count > 0: logger.info( 'Spawning {} workers'.format(required_workers_count)) spawn_workers( num=required_workers_count, target=worker, args=(config,), parent_pid=parent_pid ) else: logger.critical('Network is down. stopping workers') for c in active_children(): c.terminate() sleep(config.SLEEP)
def test_spawn_workers(self): process_mock = mock.Mock(return_value=Process_fake()) num = 3 parent_pid = 1 config = { 'test1': 1, 'test2': 2 } with mock.patch('multiprocessing.Process', process_mock): spawn_workers(num=num, target=target_fun, args=(config,), parent_pid=parent_pid) self.assertEqual(num, process_mock.call_count)
def main_loop_function(config, parent_pid): if check_network_status(config.CHECK_URL, config.HTTP_TIMEOUT): required_workers_count = config.WORKER_POOL_SIZE - len( active_children()) if required_workers_count > 0: logger.info( 'Spawning {} workers'.format(required_workers_count)) spawn_workers( num=required_workers_count, target=worker, args=(config,), parent_pid=parent_pid ) else: logger.critical('Network is down. stopping workers') for c in active_children(): c.terminate()
def test_spawn_workers(self): num = 13 target = mock.Mock() args = '' parent_pid = 13 mock_p = mock.MagicMock() mock_p.daemon = False mock_p.start = mock.Mock() mock_process = mock.Mock(return_value=mock_p) with mock.patch('lib.utils.Process', mock_process): utils.spawn_workers(num, target, args, parent_pid) assert mock_process.call_count == num self.assertTrue(mock_p.daemon)
def test_spawn_workers(self): with patch('lib.utils.Process', Mock(return_value=Mock())): utils.spawn_workers(10, 'target', 'args', 35)