Python源码示例:asyncio.get_child_watcher()
示例1
def start(self):
assert self.ensure()
self.data_path = tempfile.mkdtemp()
loop = asyncio.get_event_loop()
asyncio.get_child_watcher().attach_loop(loop)
command = (
self.daemon_bin,
f'-datadir={self.data_path}', '-printtoconsole', '-regtest', '-server', '-txindex',
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}',
f'-port={self.peerport}'
)
self.log.info(' '.join(command))
self.transport, self.protocol = await loop.subprocess_exec(
BlockchainProcess, *command
)
await self.protocol.ready.wait()
示例2
def test_scp(event_loop):
# ensure that asyncio.subprocess will work;
try:
asyncio.get_child_watcher().attach_loop(event_loop)
except RuntimeError:
pytest.skip('test_scp will always fail outside of MainThread')
async with base.CleanModel() as model:
await model.add_machine()
await asyncio.wait_for(
model.block_until(lambda: model.machines),
timeout=240)
machine = model.machines['0']
await asyncio.wait_for(
model.block_until(lambda: (machine.status == 'running' and
machine.agent_status == 'started')),
timeout=480)
with NamedTemporaryFile() as f:
f.write(b'testcontents')
f.flush()
await machine.scp_to(f.name, 'testfile', scp_opts='-p')
with NamedTemporaryFile() as f:
await machine.scp_from('testfile', f.name, scp_opts='-p')
assert f.read() == b'testcontents'
示例3
def test_ssh(event_loop):
# ensure that asyncio.subprocess will work;
try:
asyncio.get_child_watcher().attach_loop(event_loop)
except RuntimeError:
pytest.skip('test_ssh will always fail outside of MainThread')
async with base.CleanModel() as model:
app = await model.deploy('ubuntu')
await asyncio.wait_for(
model.block_until(lambda: app.units),
timeout=60)
unit = app.units[0]
await asyncio.wait_for(
model.block_until(lambda: unit.machine),
timeout=60)
machine = unit.machine
await asyncio.wait_for(
model.block_until(lambda: (machine.status == 'running' and
machine.agent_status == 'started')),
timeout=480)
output = await unit.ssh("echo test")
assert(output == "test")
示例4
def _cli_cmnd(self, *args):
cmnd_args = [
self.cli_bin, f'-datadir={self.data_path}', '-regtest',
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}'
] + list(args)
self.log.info(' '.join(cmnd_args))
loop = asyncio.get_event_loop()
asyncio.get_child_watcher().attach_loop(loop)
process = await asyncio.create_subprocess_exec(
*cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
out, _ = await process.communicate()
result = out.decode().strip()
self.log.info(result)
if result.startswith('error code'):
raise Exception(result)
return result
示例5
def __init__(self, protocol_cls, loop=None, max_workers=2,
sync_kind=TextDocumentSyncKind.INCREMENTAL):
if not issubclass(protocol_cls, asyncio.Protocol):
raise TypeError('Protocol class should be subclass of asyncio.Protocol')
self._max_workers = max_workers
self._server = None
self._stop_event = None
self._thread_pool = None
self._thread_pool_executor = None
self.sync_kind = sync_kind
if IS_WIN:
asyncio.set_event_loop(asyncio.ProactorEventLoop())
else:
asyncio.set_event_loop(asyncio.SelectorEventLoop())
self.loop = loop or asyncio.get_event_loop()
try:
asyncio.get_child_watcher().attach_loop(self.loop)
except NotImplementedError:
pass
self.lsp = protocol_cls(self)
示例6
def __init__(self, *args, **kwargs):
self.loop = kwargs.pop('loop', asyncio.get_event_loop())
asyncio.get_child_watcher().attach_loop(self.loop)
self.dev_mode = kwargs.pop('dev_mode', False)
self.token = os.getenv('bot_token') if not self.dev_mode else os.getenv('bot_beta_token')
self.self_bot = kwargs.pop('self_bot', False)
if self.self_bot:
self.token = os.getenv('notsosuper_token')
shard_id = kwargs.get('shard_id', 0)
command_prefix = kwargs.pop('command_prefix', commands.when_mentioned_or('.'))
init_logging(shard_id, self)
super().__init__(command_prefix=command_prefix, *args, **kwargs)
self.remove_command('help')
init_funcs(self)
self.owner = None
self.start_time = time.time()
self.own_task = None
self.last_message = None
self.command_messages = {}
示例7
def __init__(self, name="Asyncio"):
self.logger = logging.getLogger('moler.asyncio-loop-thrd')
self.ev_loop = asyncio.new_event_loop()
# to allow subprocesses running in "subthread"
# otherwise we get error:
# RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
# This is because unix watchers embed signal handles used to stop subprocesses
#
# https://stackoverflow.com/questions/28915607/does-asyncio-support-running-a-subprocess-from-a-non-main-thread/28917653#28917653
# When asyncio starts subprocess it need to be notified by subproc finish event.
# Unfortunately in Unix systems the generic way to do it is catching SIG_CHLD signal.
# Python interpreter can process signals only in main thread.
# answer by: https://stackoverflow.com/users/3454879/andrew-svetlov
#
asyncio.get_child_watcher().attach_loop(self.ev_loop)
self.ev_loop.set_debug(enabled=True)
self.logger.debug("created asyncio loop: {}:{}".format(id(self.ev_loop), self.ev_loop))
self.ev_loop_done = AsyncioEventThreadsafe(loop=self.ev_loop)
self.ev_loop_done.clear()
self.ev_loop_started = threading.Event()
super(AsyncioLoopThread, self).__init__(target=self._start_loop,
done_event=self.ev_loop_done,
kwargs={'loop': self.ev_loop,
'loop_started': self.ev_loop_started,
'loop_done': self.ev_loop_done})
# Thread-3 --> [Thread, 3]
name_parts = self.name.split('-')
self.name = "{}-{}".format(name, name_parts[-1])
self.logger.debug("created thread {} for asyncio loop".format(self))
示例8
def _cli_cmnd(self, *args):
cmnd_args = [
self.cli_bin, f'-datadir={self.data_path}', '-regtest',
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}'
] + list(args)
self.log.info(' '.join(cmnd_args))
loop = asyncio.get_event_loop()
asyncio.get_child_watcher().attach_loop(loop)
process = await asyncio.create_subprocess_exec(
*cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
out, _ = await process.communicate()
self.log.info(out.decode().strip())
return out.decode().strip()
示例9
def test_scp(event_loop):
# ensure that asyncio.subprocess will work;
try:
asyncio.get_child_watcher().attach_loop(event_loop)
except RuntimeError:
pytest.skip('test_scp will always fail outside of MainThread')
async with base.CleanModel() as model:
app = await model.deploy('ubuntu')
await asyncio.wait_for(
model.block_until(lambda: app.units),
timeout=60)
unit = app.units[0]
await asyncio.wait_for(
model.block_until(lambda: unit.machine),
timeout=60)
machine = unit.machine
await asyncio.wait_for(
model.block_until(lambda: (machine.status == 'running' and
machine.agent_status == 'started')),
timeout=480)
with NamedTemporaryFile() as f:
f.write(b'testcontents')
f.flush()
await unit.scp_to(f.name, 'testfile')
with NamedTemporaryFile() as f:
await unit.scp_from('testfile', f.name)
assert f.read() == b'testcontents'
示例10
def start(self):
assert self.ensure()
self.data_path = tempfile.mkdtemp()
loop = asyncio.get_event_loop()
asyncio.get_child_watcher().attach_loop(loop)
command = [
self.daemon_bin,
f'-datadir={self.data_path}', '-printtoconsole', '-regtest', '-server', '-txindex',
f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}',
f'-port={self.peerport}'
]
self.log.info(' '.join(command))
while not self.stopped:
if self.running.is_set():
await asyncio.sleep(1)
continue
await self.restart_ready.wait()
try:
self.transport, self.protocol = await loop.subprocess_exec(
BlockchainProcess, *command
)
await self.protocol.ready.wait()
assert not self.protocol.stopped.is_set()
self.running.set()
except asyncio.CancelledError:
self.running.clear()
raise
except Exception as e:
self.running.clear()
log.exception('failed to start lbrycrdd', exc_info=e)
示例11
def __init__(self):
print("Initialisation of the scheduler manager")
self.scheduler = BackgroundScheduler()
# create the async loop in the main thread
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop) # bind event loop to current thread
asyncio.get_child_watcher().attach_loop(self.loop)
示例12
def test_close_dont_kill_finished(self):
@asyncio.coroutine
def kill_running():
create = self.loop.subprocess_exec(asyncio.SubprocessProtocol,
*PROGRAM_BLOCKED)
transport, protocol = yield from create
proc = transport.get_extra_info('subprocess')
# kill the process (but asyncio is not notified immediately)
proc.kill()
proc.wait()
proc.kill = mock.Mock()
proc_returncode = proc.poll()
transport_returncode = transport.get_returncode()
transport.close()
return (proc_returncode, transport_returncode, proc.kill.called)
# Ignore "Unknown child process pid ..." log of SafeChildWatcher,
# emitted because the test already consumes the exit status:
# proc.wait()
with test_utils.disable_logger():
result = self.loop.run_until_complete(kill_running())
test_utils.run_briefly(self.loop)
proc_returncode, transport_return_code, killed = result
self.assertIsNotNone(proc_returncode)
self.assertIsNone(transport_return_code)
# transport.close() must not kill the process if it finished, even if
# the transport was not notified yet
self.assertFalse(killed)
# Unlike SafeChildWatcher, FastChildWatcher does not pop the
# callbacks if waitpid() is called elsewhere. Let's clear them
# manually to avoid a warning when the watcher is detached.
if sys.platform != 'win32' and \
isinstance(self, SubprocessFastWatcherTests):
asyncio.get_child_watcher()._callbacks.clear()
示例13
def _connect_child(self, argv):
if os.name != 'nt':
self._child_watcher = asyncio.get_child_watcher()
self._child_watcher.attach_loop(self._loop)
coroutine = self._loop.subprocess_exec(self._fact, *argv)
self._loop.run_until_complete(coroutine)
示例14
def test_close_dont_kill_finished(self):
async def kill_running():
create = self.loop.subprocess_exec(asyncio.SubprocessProtocol,
*PROGRAM_BLOCKED)
transport, protocol = await create
proc = transport.get_extra_info('subprocess')
# kill the process (but asyncio is not notified immediately)
proc.kill()
proc.wait()
proc.kill = mock.Mock()
proc_returncode = proc.poll()
transport_returncode = transport.get_returncode()
transport.close()
return (proc_returncode, transport_returncode, proc.kill.called)
# Ignore "Unknown child process pid ..." log of SafeChildWatcher,
# emitted because the test already consumes the exit status:
# proc.wait()
with test_utils.disable_logger():
result = self.loop.run_until_complete(kill_running())
test_utils.run_briefly(self.loop)
proc_returncode, transport_return_code, killed = result
self.assertIsNotNone(proc_returncode)
self.assertIsNone(transport_return_code)
# transport.close() must not kill the process if it finished, even if
# the transport was not notified yet
self.assertFalse(killed)
# Unlike SafeChildWatcher, FastChildWatcher does not pop the
# callbacks if waitpid() is called elsewhere. Let's clear them
# manually to avoid a warning when the watcher is detached.
if (sys.platform != 'win32' and
isinstance(self, SubprocessFastWatcherTests)):
asyncio.get_child_watcher()._callbacks.clear()