def test_process_command_no_thread(echo_tool, mock_is_windows, mocker, monkeypatch): read_callback = MagicMock() csharp_subprocess.IO_LOOP = None if platform.system() == "Windows": asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) mock_set_event: MagicMock = mocker.patch("asyncio.set_event_loop_policy") loop_policy = MagicMock() monkeypatch.setattr(asyncio, "WindowsProactorEventLoopPolicy", loop_policy, raising=False) # Run csharp_subprocess.process_command( [sys.executable, str(echo_tool)], "hello\r\nthis is a nice world\r\n\r\nWe some crazy stuff.", read_callback, add_mono_if_needed=False, ) # Assert read_callback.assert_has_calls([ call("hello"), call("this is a nice world"), call("We some crazy stuff."), ]) if mock_is_windows: mock_set_event.assert_called_once_with(loop_policy.return_value) else: mock_set_event.assert_not_called()
def main(argv=None): if argv is None: argv = sys.argv else: sys.argv.extend(argv) # Setup argument parser parser = ArgumentParser(description="AFL hang analyzer)", formatter_class=RawDescriptionHelpFormatter) parser.add_argument("-e", "--exe", dest="exe", action="store", default="/home/happy/afl_chengang/build/bin/wavm", help="Exe name to run") parser.add_argument("-j", "--job", dest="job", action="store", type=int, default=1, help="Job limit") args = parser.parse_args() if args.exe and args.job: fdf = FileDuplicateFinder(args.exe, args.job) start = time.time() if platform.system() == 'Windows': asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) fdf.find_unique_contents() end = time.time() rounded_end = ('{0:.4f}'.format(round(end - start, 4))) print('Script ran in about', str(rounded_end), 'seconds') return 0
def create_and_run_plugin(plugin_class, argv): """Call this method as an entry point for the implemented integration. :param plugin_class: your plugin class. :param argv: command line arguments with which the script was started. Example of possible use of the method: .. code-block:: python :linenos: def main(): create_and_run_plugin(PlatformPlugin, sys.argv) if __name__ == "__main__": main() """ if len(argv) < 3: logger.critical("Not enough parameters, required: token, port") sys.exit(1) token = argv[1] try: port = int(argv[2]) except ValueError: logger.critical("Failed to parse port value: %s", argv[2]) sys.exit(2) if not (1 <= port <= 65535): logger.critical("Port value out of range (1, 65535)") sys.exit(3) if not issubclass(plugin_class, Plugin): logger.critical("plugin_class must be subclass of Plugin") sys.exit(4) async def coroutine(): reader, writer = await asyncio.open_connection("127.0.0.1", port) try: extra_info = writer.get_extra_info("sockname") logger.info("Using local address: %s:%u", *extra_info) async with plugin_class(reader, writer, token) as plugin: await plugin.run() finally: try: writer.close() await writer.wait_closed() except (ConnectionAbortedError, ConnectionResetError): pass try: if sys.platform == "win32": asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(coroutine()) except Exception: logger.exception("Error while running plugin") sys.exit(5)
def setup_asyncio(config: util.config.Config) -> asyncio.AbstractEventLoop: """Returns a new asyncio event loop with settings from the given config.""" asyncio_config: util.config.AsyncIOConfig = config["asyncio"] if sys.platform == "win32": # Force ProactorEventLoop on Windows for subprocess support policy = asyncio.WindowsProactorEventLoopPolicy() asyncio.set_event_loop_policy(policy) elif not asyncio_config["disable_uvloop"]: # Initialize uvloop if available try: # noinspection PyUnresolvedReferences import uvloop uvloop.install() log.info("Using uvloop event loop") except ImportError: pass loop = asyncio.get_event_loop() if asyncio_config["debug"]: log.info("Enabling asyncio debug mode") loop.set_debug(True) return loop
def run(self): """Starts execution *in current thread*. .. note:: You probably want :func:`~Job.start`. """ if os.name == 'nt': asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) try: self.loop = asyncio.get_event_loop() except RuntimeError: self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) if os.name == 'nt': assert isinstance( self.loop, asyncio.ProactorEventLoop), 'bad event loop for Windows' returncode = self.loop.run_until_complete(self._main()) self._debug('Finishing job') for q in self._queue.values(): q.put(None) return returncode
def run(self, mac=True, vendor=False): """ Method used to create the task lists and to run the coroutine loop """ # By default at the beginning of every scan there is no host found self.list_of_hosts_found = [] self.my_tasks = [] # The list of list my_list_of_tasks groups lists of coroutines self.my_list_of_tasks = [] # By default the current list is added to the list of list my_list_of_tasks # A very important concept is that filling the empty list my_tasks will # also fill the current list of lists my_list_of_tasks. self.my_list_of_tasks.append(self.my_tasks) hosts = list(self.network.hosts()) if self.network.num_addresses == 1: hosts = [self.network.network_address] # Create the coroutines tasks for host in hosts: # my_tasks is a list with coroutine tasks. It gets 2 parameters: one with # the ping command and the other one with the ip address of the target self.my_tasks.append(self.ping_coroutine(str(host), mac, vendor)) # if Windows is in use then these commands are needed otherwise # "asyncio.create_subprocess_shell" will fail if self.system == "windows": asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) # Run the coroutine loop asyncio.run(self.run_coroutins())
def _async_thread_handler(self) -> None: logger.debug("_async_thread_handler") try: asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self.async_loop = loop self.message_queue = asyncio.Queue() self.sub_queue = asyncio.Queue() login_request = self._build_login_request() loop.run_until_complete( self.conn.start(self.websocket_url, login_request)) self.connect_event.set() send_messages_coro = self._send_message_async() send_sub_coro = self._send_sub_message_async() recv_messages_coro = self._recv_message_async() loop.run_until_complete( asyncio.gather(send_messages_coro, send_sub_coro, recv_messages_coro)) loop.close() except websockets.exceptions.ConnectionClosedOK: pass except Exception as e: logger.error("_async_thread_handler: {}".format(repr(e)), exc_info=True) self.connect_event.set()
def proactor_loop(): # type: ignore policy = asyncio.WindowsProactorEventLoopPolicy() # type: ignore asyncio.set_event_loop_policy(policy) with loop_context(policy.new_event_loop) as _loop: asyncio.set_event_loop(_loop) yield _loop
def set_loop() -> None: """Attempt to use uvloop.""" import asyncio from asyncio.events import BaseDefaultEventLoopPolicy policy = None if sys.platform == 'win32': if hasattr(asyncio, 'WindowsProactorEventLoopPolicy'): # pylint: disable=no-member policy = asyncio.WindowsProactorEventLoopPolicy() else: class ProactorPolicy(BaseDefaultEventLoopPolicy): """Event loop policy to create proactor loops.""" _loop_factory = asyncio.ProactorEventLoop policy = ProactorPolicy() else: try: import uvloop except ImportError: pass else: policy = uvloop.EventLoopPolicy() if policy is not None: asyncio.set_event_loop_policy(policy)
def proactor_loop(): # type: ignore[no-untyped-def] policy = asyncio.WindowsProactorEventLoopPolicy() # type: ignore[attr-defined] asyncio.set_event_loop_policy(policy) with loop_context(policy.new_event_loop) as _loop: asyncio.set_event_loop(_loop) yield _loop
def update_thread_handler(self) -> None: logging.info("update_thread_handler start") try: pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED) asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self.async_loop = loop self.update_message_queue = asyncio.Queue(loop=self.async_loop) self.send_message_queue = asyncio.Queue(loop=self.async_loop) self.ready_to_send = asyncio.Event(loop=self.async_loop) # Following call can cause deadlock if mainthread is not pumping Windows message. self.SetCallbackThread() update_msg_coro = self._update_msg_handler() send_msg_coro = self._send_msg_handler() loop.run_until_complete( asyncio.gather(update_msg_coro, send_msg_coro)) loop.close() except Exception as e: logging.error("update_thread_handler: {}".format(repr(e))) finally: pythoncom.CoUninitialize()
def run_as_standalone(self, future: Any) -> None: """ Run as a standalone application """ if platform.system() == "Windows": signal.signal(signal.SIGBREAK, lambda signal_number, frame: self. shutdown()) # pylint: disable = no-member signal.signal(signal.SIGINT, lambda signal_number, frame: self.shutdown()) signal.signal(signal.SIGTERM, lambda signal_number, frame: self.shutdown()) if platform.system() == "Windows": asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy( )) # pylint: disable = no-member exit_code = EXIT_SUCCESS logger.info("%s %s", self.title, self.version) try: self.run(future) except SystemExit as exception: exit_code = exception.code logger.info("SystemExit", exc_info=True) except: # pylint: disable = bare-except exit_code = EXIT_FAILURE logger.error("Unhandled exception", exc_info=True) logger.info("Exit with code %s", exit_code) sys.exit(exit_code)
def cli(verbosity): "Mutation testing for Python3" logging_level = getattr(logging, verbosity) logging.basicConfig(level=logging_level, handlers=[RichHandler()]) if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
def main(): if WINDOWS: asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) try: asyncio.run(amain()) except (KeyboardInterrupt, SystemExit) as e: rootlogger.info('Received %r', e)
def _process_command(args: List[str], input_data: str, read_callback: Callable[[str], None]): work = _process_command_async(args, input_data, read_callback) if IO_LOOP is None: if _is_windows(): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(work) else: asyncio.run_coroutine_threadsafe(work, IO_LOOP).result()
def ensure_asyncio_event_loop_compatibility_for_windows(): """ WindowsProactorEventLoopPolicy is required for asyncio.create_subprocess_exec or it will fail with NotImplementedError. It is default in Python 3.8 but not in older versions. """ if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
def get_default_policy(): if sys.platform == 'win32': return asyncio.WindowsProactorEventLoopPolicy() # TODO: evaluate usage of uvloop # with contextlib.suppress(ModuleNotFoundError): # import uvloop # return uvloop.EventLoopPolicy() return asyncio.DefaultEventLoopPolicy()
def event_loop(request): """ Run all tests using the default event loop and never closes it. """ if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) yield loop loop.close()
def proactor_loop(): # type: ignore if not PY_37: policy = asyncio.get_event_loop_policy() policy._loop_factory = asyncio.ProactorEventLoop # type: ignore else: policy = asyncio.WindowsProactorEventLoopPolicy() # type: ignore asyncio.set_event_loop_policy(policy) with loop_context(policy.new_event_loop) as _loop: asyncio.set_event_loop(_loop) yield _loop
def ws_search(query_list, group, query_type): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # 在django中调用时需要设置loop params = { 'query_list': query_list, 'group': group, 'query_type': query_type } return asyncio.get_event_loop().run_until_complete(search(params))
def __init__(self, items, subproc_count=multiprocessing.cpu_count(), verbose=False): item_queue = asyncio.Queue() for item in items: item_queue.put_nowait(item) self.items = items self.subproc_count = subproc_count self.verbose = verbose if 'win32' in sys.platform: # Windows specific event-loop policy & cmd asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
def test_proactor_win_policy(self): async def main(): self.assertIsInstance(asyncio.get_running_loop(), asyncio.ProactorEventLoop) old_policy = asyncio.get_event_loop_policy() try: asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(main()) finally: asyncio.set_event_loop_policy(old_policy)
def main(): if not os.path.exists('json'): os.makedirs('json') # without create loop we get raise "RuntimeError: Event loop is closed" if sys.version_info[:2] == (3, 7): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) loop = asyncio.get_event_loop() try: loop.run_until_complete(get_data()) loop.run_until_complete(asyncio.sleep(0.1)) finally: loop.close()
def _update_event_loop_policy(): if _sys.platform == "win32": _asyncio.set_event_loop_policy( _asyncio.WindowsProactorEventLoopPolicy()) elif _sys.implementation.name == "cpython": # Let's not force this dependency, uvloop is much faster on cpython try: import uvloop as _uvloop except ImportError: pass else: _asyncio.set_event_loop_policy(_uvloop.EventLoopPolicy())
def main(args=None): if args is None: # main function shouldn't take arguments if using setuptools. # Hence, we read arguments from sys.argv args = sys.argv[1:] options = parse_options(args) if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(run(options))
def patch_popen(): nox_popen_module.popen = patched_popen from nox.command import popen if popen is not patched_popen: nox.command.popen = patched_popen # change event loop on windows # see https://stackoverflow.com/a/44639711/7262247 # and https://docs.python.org/3/library/asyncio-platforms.html#subprocess-support-on-windows if 'win32' in sys.platform: # Windows specific event-loop policy & cmd asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
def test_event_loop_policy_3_7(self, version_info): version_info.__getitem__.side_effect = lambda x: [3, 7][x] # fake a manual 3.8 default setup asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) reload(config) self.assertIsInstance( asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy, "Asyncio event loop policy should be WindowsProactorEventLoopPolicy." )
def set_loop_policy() -> None: """ Try to use a ProactorEventLoop on Windows and uvloop elsewhere """ if (sys.platform == "win32" and hasattr(asyncio, "WindowsProactorEventLoopPolicy")): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) else: with suppress(ImportError): import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
def __init__( self, host: str = None, port: int = None, app: ASGIApp = None, config: Config = None, ): if os.name != "nt": multiprocessing.set_start_method("fork") self.is_posix = True else: asyncio.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) self.is_posix = False self.config: Config = config or config_init() setLoggerClass(QactuarLogger) dictConfig(self.config.LOGS) self.server_log: Logger = getLogger("qt_server") self.exception_log: Logger = getLogger("qt_exception") self.host: str = host or self.config.HOST self.port: int = port or self.config.PORT self.scheme: str = "http" self.listen_socket: socket.socket = socket.socket( self.address_family, self.socket_type) self.listen_socket.setsockopt(self.socket_level, self.socket_opt_name, 1) self.listen_socket.bind((self.host, self.port)) self.listen_socket.listen(self.request_queue_size) self.ssl_context: Optional[ssl.SSLContext] = None if self.config.SSL_CERT_PATH and self.config.SSL_KEY_PATH: self.setup_ssl() self.server_name: str = socket.getfqdn(self.host) self.server_port: int = self.port self.client_info: Tuple[str, int] = ("", 0) self.loop = asyncio.get_event_loop() self.processes: Dict[int, multiprocessing.Process] = {} self.shutting_down: bool = False self.lifespan_handler: LifespanHandler = LifespanHandler(self) self.apps: Dict[str, ASGIApp] = {"/": app} if app else {} for route, app_path in self.config.APPS.items(): module_str, app_str = app_path.split(":") app_module = import_module(module_str) self.apps[route] = getattr(app_module, app_str)
def compat_event_loop(): """OS agnostic context manager for an event loop.""" if sys.platform.startswith("win"): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) event_loop = asyncio.get_event_loop() if event_loop.is_closed(): event_loop = asyncio.new_event_loop() asyncio.set_event_loop(event_loop) yield event_loop event_loop.close()