def main(cls, loop=None, argv=sys.argv): """ Runs cli commands in asyncio loop and outputs in appropriate format """ if loop is None: loop = asyncio.get_event_loop() # In Python 3.8 ThreadedChildWatcher becomes the default which # should work fine for us. However, in Python 3.7 SafeChildWatcher # is the default and may cause BlockingIOErrors when many # subprocesses are created # https://docs.python.org/3/library/asyncio-policy.html#asyncio.FastChildWatcher if ( sys.version_info.major == 3 and sys.version_info.minor == 7 and sys.platform != "win32" ): watcher = asyncio.FastChildWatcher() asyncio.set_child_watcher(watcher) watcher.attach_loop(loop) result = None try: result = loop.run_until_complete(cls._main(*argv[1:])) except KeyboardInterrupt: # pragma: no cover pass # pragma: no cover loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def __init__(self, *args, **kwargs): hostlist_sanity_check() self.result_queue = Queue() logger.info("initializing HttpSource") super().__init__(*args, **kwargs) watcher = asyncio.FastChildWatcher() watcher.attach_loop(self.event_loop) asyncio.set_child_watcher(watcher)
def __init__(self, *args, **kwargs): logger.info("initializing IpmiSource") super().__init__(*args, **kwargs) self.period = None self.result_queue = Queue() watcher = asyncio.FastChildWatcher() watcher.attach_loop(self.event_loop) asyncio.set_child_watcher(watcher)
def test_get_child_watcher_to_override_existing_one(self): watcher = asyncio.FastChildWatcher() # initializes default watcher as side-effect self.policy.get_child_watcher() self.policy.set_child_watcher(watcher) self.assertIs(self.policy._watcher, watcher) self.assertIs(watcher, self.policy.get_child_watcher())
def __init__(self, *args, **kwargs): logger.info("initializing IpmiSource") super().__init__(*args, **kwargs) self.result_queue = Queue() self.collection_loops: Set[asyncio.Task] = set() self.log_loop: Optional[asyncio.Task] = None watcher = asyncio.FastChildWatcher() watcher.attach_loop(self.event_loop) asyncio.set_child_watcher(watcher)
def main(cls, loop=None, argv=sys.argv): """ Runs cli commands in asyncio loop and outputs in appropriate format """ if loop is None: # In order to use asyncio.subprocess_create_exec from event loops in # non-main threads we have to call asyncio.get_child_watcher(). This # is only for Python 3.7 if (sys.version_info.major == 3 and sys.version_info.minor == 7 and sys.platform != "win32"): asyncio.get_child_watcher() # Create a new event loop loop = asyncio.get_event_loop() # In Python 3.8 ThreadedChildWatcher becomes the default which # should work fine for us. However, in Python 3.7 SafeChildWatcher # is the default and may cause BlockingIOErrors when many # subprocesses are created # https://docs.python.org/3/library/asyncio-policy.html#asyncio.FastChildWatcher if (sys.version_info.major == 3 and sys.version_info.minor == 7 and sys.platform != "win32"): watcher = asyncio.FastChildWatcher() asyncio.set_child_watcher(watcher) watcher.attach_loop(loop) result = None try: result = loop.run_until_complete(cls._main(*argv[1:])) if (result is not None and result is not DisplayHelp and result is not CMDOutputOverride and result != [CMDOutputOverride]): json.dump( export_dict(result=result)["result"], sys.stdout, sort_keys=True, indent=4, separators=(",", ": "), cls=cls.JSONEncoder, ) print() except KeyboardInterrupt: # pragma: no cover pass # pragma: no cover loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def get_event_loop(): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # On Windows the default SelectorEventLoop is not available: # https://docs.python.org/3.5/library/asyncio-subprocess.html#windows-event-loop if sys.platform == 'win32' and \ not isinstance(loop, asyncio.ProactorEventLoop): loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) # Avoid spammy BlockingIOError warnings with older python versions if sys.platform != 'win32' and \ sys.version_info < (3, 8, 0): asyncio.set_child_watcher(asyncio.FastChildWatcher()) asyncio.get_child_watcher().attach_loop(loop) return loop
from pathlib import Path from nonocaptcha import util from nonocaptcha.proxy import ProxyDB from nonocaptcha.solver import Solver SECRET_KEY = "CHANGEME" proxies = ProxyDB(last_banned_timeout=45 * 60) # This is 45 minutes proxy_source = None # Can be URL or file location proxy_username, proxy_password = (None, None) parent_loop = asyncio.get_event_loop() # I'm not sure exactly if FastChildWatcher() is really any faster, requires # further research. asyncio.set_child_watcher(asyncio.FastChildWatcher()) asyncio.get_child_watcher().attach_loop(parent_loop) app = web.Application() # Clear Chrome temporary profiles dir = f"{Path.home()}/.pyppeteer/.dev_profile" shutil.rmtree(dir, ignore_errors=True) # Should be less crash prone since we use the main loop, only spawning the # task in a future within an executor. Maybe. class TaskRerun(object): def __init__(self, coro, duration): self.coro = coro self.duration = duration
def test_get_child_watcher_after_set(self): watcher = asyncio.FastChildWatcher() self.policy.set_child_watcher(watcher) self.assertIs(self.policy._watcher, watcher) self.assertIs(watcher, self.policy.get_child_watcher())