Beispiel #1
0
    def run(self, debug=False):

        if events._get_running_loop() is not None:
            raise RuntimeError("There is already a running event loop")

        async def runner():
            try:
                await self.start()
            finally:
                await self.close()

        loop = self.loop
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            loop.run_until_complete(runner())
        except KeyboardInterrupt:
            self.logger.info("Received signal to terminate tasks and event loop.")
        finally:
            try:
                self.logger.debug("Cleaning up tasks.")
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
                # loop.run_until_complete(loop.shutdown_default_executor())
            finally:
                events.set_event_loop(None)
                self.logger.info("Closing event loop.")
Beispiel #2
0
    def run_forever(self):
        self._check_closed()
        old_thread_id = self._thread_id
        old_running_loop = events._get_running_loop()
        set_coro_tracking(self._debug)
        self._thread_id = threading.get_ident()

        old_agen_hooks = sys.get_asyncgen_hooks()
        if self._asyncgens is not None:
            sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                                   finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                try:
                    self._run_once()
                except IndexError:
                    # Ignore 'pop from an empty deque' errors.
                    # This happens when all ready handles have already been
                    # processed but _run_once expects there to be more.
                    # Since the handles have been processed anyway it is
                    # safe to ignore this.
                    pass
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = old_thread_id
            events._set_running_loop(old_running_loop)
            set_coro_tracking(False)
            if self._asyncgens is not None:
                sys.set_asyncgen_hooks(*old_agen_hooks)
Beispiel #3
0
    def run_forever(self):
        if sys.version_info >= (3, 7, 0):
            set_coro_tracking = self._set_coroutine_origin_tracking
        else:
            set_coro_tracking = self._set_coroutine_wrapper

        self._check_closed()
        old_thread_id = self._thread_id
        old_running_loop = events._get_running_loop()
        set_coro_tracking(self._debug)
        self._thread_id = threading.get_ident()

        if self._asyncgens is not None:
            old_agen_hooks = sys.get_asyncgen_hooks()
            sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                                   finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = old_thread_id
            events._set_running_loop(old_running_loop)
            set_coro_tracking(False)
            if self._asyncgens is not None:
                sys.set_asyncgen_hooks(*old_agen_hooks)
Beispiel #4
0
 def manage_run(self):
     """Set up the loop for running."""
     self._check_closed()
     old_thread_id = self._thread_id
     old_running_loop = events._get_running_loop()
     try:
         self._thread_id = threading.get_ident()
         events._set_running_loop(self)
         self._num_runs_pending += 1
         if self._is_proactorloop:
             if self._self_reading_future is None:
                 self.call_soon(self._loop_self_reading)
         yield
     finally:
         self._thread_id = old_thread_id
         events._set_running_loop(old_running_loop)
         self._num_runs_pending -= 1
         if self._is_proactorloop:
             if (self._num_runs_pending == 0
                     and self._self_reading_future is not None):
                 ov = self._self_reading_future._ov
                 self._self_reading_future.cancel()
                 if ov is not None:
                     self._proactor._unregister(ov)
                 self._self_reading_future = None
Beispiel #5
0
    def run_forever(self, app_context):
        """Set up the asyncio event loop, integrate it with the Winforms
        event loop, and start the application.

        This largely duplicates the setup behavior of the default Proactor
        run_forever implementation.

        :param app_context: The WinForms.ApplicationContext instance
            controlling the lifecycle of the app.
        """
        # Remember the application context.
        self.app_context = app_context

        # Register a custom user window message.
        self.msg_id = user32.RegisterWindowMessageA("Python asyncio tick")
        # Add a message filter to listen for the asyncio tick message
        # FIXME: Actually install the message filter.
        # msg_filter = AsyncIOTickMessageFilter(self, self.msg_id)
        # WinForms.Application.AddMessageFilter(msg_filter)

        # Setup the Proactor.
        # The code between the following markers should be exactly the same as
        # the official CPython implementation, up to the start of the
        # `while True:` part of run_forever() (see BaseEventLoop.run_forever()
        # in Lib/ascynio/base_events.py)
        # === START BaseEventLoop.run_forever() setup ===
        self._check_closed()
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')
        self._set_coroutine_origin_tracking(self._debug)
        self._thread_id = threading.get_ident()
        try:
            self._old_agen_hooks = sys.get_asyncgen_hooks()
            sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                                   finalizer=self._asyncgen_finalizer_hook)
        except AttributeError:
            # Python < 3.6 didn't have sys.get_asyncgen_hooks();
            # No action required for those versions.
            pass

        events._set_running_loop(self)
        # === END BaseEventLoop.run_forever() setup ===

        # Rather than going into a `while True:` loop, we're going to use the
        # Winforms event loop to queue a tick() message that will cause a
        # single iteration of the asyncio event loop to be executed. Each time
        # we do this, we queue *another* tick() message in 5ms time. In this
        # way, we'll get a continuous stream of tick() calls, without blocking
        # the Winforms event loop.

        # Queue the first asyncio tick.
        self.enqueue_tick()

        # Start the Winforms event loop.
        WinForms.Application.Run(self.app_context)
Beispiel #6
0
 def run(main, *, debug=False):
     loop = events._get_running_loop()
     if not loop:
         loop = events.new_event_loop()
         events.set_event_loop(loop)
         _patch_loop(loop)
     loop.set_debug(debug)
     task = asyncio.ensure_future(main)
     try:
         return loop.run_until_complete(task)
     finally:
         if not task.done():
             task.cancel()
             with suppress(asyncio.CancelledError):
                 loop.run_until_complete(task)
Beispiel #7
0
 async def to_thread(func, *args, **kwargs):
     """Asynchronously run function *func* in a separate thread.
     Any *args and **kwargs supplied for this function are directly passed
     to *func*. Also, the current :class:`contextvars.Context` is propogated,
     allowing context variables from the main thread to be accessed in the
     separate thread.
     Return a coroutine that can be awaited to get the eventual result of *func*.
     """
     import contextvars
     import functools
     from asyncio import events
     loop = events._get_running_loop()
     ctx = contextvars.copy_context()
     func_call = functools.partial(ctx.run, func, *args, **kwargs)
     return await loop.run_in_executor(None, func_call)
Beispiel #8
0
    def native_run(main, *, debug=False):
        # Snatched from Python 3.7
        from asyncio import coroutines
        from asyncio import events
        from asyncio import tasks

        def _cancel_all_tasks(loop):
            to_cancel = all_tasks(loop)
            if not to_cancel:
                return

            for task in to_cancel:
                task.cancel()

            loop.run_until_complete(
                tasks.gather(*to_cancel, loop=loop, return_exceptions=True))

            for task in to_cancel:
                if task.cancelled():
                    continue
                if task.exception() is not None:
                    loop.call_exception_handler({
                        'message':
                        'unhandled exception during asyncio.run() shutdown',
                        'exception': task.exception(),
                        'task': task,
                    })

        if events._get_running_loop() is not None:
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop")

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            return loop.run_until_complete(main)
        finally:
            try:
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
            finally:
                events.set_event_loop(None)
                loop.close()
Beispiel #9
0
    def native_run(main: Awaitable[_T], *, debug: bool = False) -> _T:
        """Run a coroutine.

        This function runs the passed coroutine, taking care of
        managing the asyncio event loop and finalizing asynchronous
        generators.

        This function cannot be called when another asyncio event loop is
        running in the same thread.

        If debug is True, the event loop will be run in debug mode.

        This function always creates a new event loop and closes it at the end.
        It should be used as a main entry point for asyncio programs, and should
        ideally only be called once.

        Example:

            async def main():
                await asyncio.sleep(1)
                print('hello')

            asyncio.run(main())
        """
        from asyncio import events, coroutines

        if events._get_running_loop() is not None:
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop")

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            return loop.run_until_complete(main)
        finally:
            try:
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
            finally:
                events.set_event_loop(None)  # type: ignore
                loop.close()
Beispiel #10
0
 def run_until_complete(self, future):
     old_running_loop = events._get_running_loop()
     try:
         self._check_closed()
         events._set_running_loop(self)
         f = asyncio.ensure_future(future, loop=self)
         if f is not future:
             f._log_destroy_pending = False
         while not f.done():
             self._run_once()
             if self._stopping:
                 break
         if not f.done():
             raise RuntimeError(
                 'Event loop stopped before Future completed.')
         return f.result()
     finally:
         events._set_running_loop(old_running_loop)
Beispiel #11
0
    def run(self):
        """Internal implementatin of run using the CoreFoundation event loop."""
        recursive = self.is_running()
        if not recursive and hasattr(events, "_get_running_loop") and events._get_running_loop():
            raise RuntimeError('Cannot run the event loop while another loop is running')

        if not recursive:
            self._running = True
            if hasattr(events, "_set_running_loop"):
                events._set_running_loop(self)

        try:
            self._lifecycle.start()
        finally:
            if not recursive:
                self._running = False
                if hasattr(events, "_set_running_loop"):
                    events._set_running_loop(None)
Beispiel #12
0
    def async_run(main):
        """
        A simplified version of Python 3.7+ run
        """
        if events._get_running_loop() is not None:  # pylint: disable=protected-access
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop"
            )

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            return loop.run_until_complete(main)
        finally:
            events.set_event_loop(None)
            loop.close()
def _run(main: Coroutine, *, debug: bool = False) -> int:
    """Performs the logic of running an automation's main coroutine and ensuring
    the exit code it returns is used as the processes exit code, as well as, properly
    shutting down the event loop

    :param main: An automation's main coroutine
    :param debug: Should the event loop be run in debug mode
    :return: The exit code returned by the automation's main coroutine
    """
    if events._get_running_loop() is not None:
        raise RuntimeError(
            "run_automation() cannot be called from a running event loop")

    if not coroutines.iscoroutine(main):
        raise ValueError("a coroutine was expected, got {!r}".format(main))
    loop = asyncio.get_event_loop()
    try:
        loop.set_debug(debug)
        result = loop.run_until_complete(main)
        logger.info(f"run_automation: exiting with code {result}")
        return result
    except Exception as e:
        logger.exception(
            "run_automation: While running an automation an exception was thrown",
            exc_info=e,
        )
        return 2
    except KeyboardInterrupt as e:
        logger.exception(
            "run_automation: While running an automation an KeyboardInterrupt happened",
            exc_info=e,
        )
        return 0
    finally:
        try:
            _cancel_all_tasks(loop)
            loop.run_until_complete(loop.shutdown_asyncgens())
        except Exception:
            pass
        finally:
            loop.close()
Beispiel #14
0
    def run(self):
        recursive = self.is_running()
        if not recursive and hasattr(events, "_get_running_loop") and events._get_running_loop():
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')

        if not recursive:
            self._running = True
            if hasattr(events, "_set_running_loop"):
                events._set_running_loop(self)

        try:
            if self._application is not None:
                self._application.run(None)
            else:
                self._mainloop.run()
        finally:
            if not recursive:
                self._running = False
                if hasattr(events, "_set_running_loop"):
                    events._set_running_loop(None)
Beispiel #15
0
    def run(self):
        recursive = self.is_running()
        if not recursive and hasattr(events, "_get_running_loop") and events._get_running_loop():
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')

        if not recursive:
            self._running = True
            if hasattr(events, "_set_running_loop"):
                events._set_running_loop(self)

        try:
            if self._application is not None:
                self._application.run(None)
            else:
                self._mainloop.run()
        finally:
            if not recursive:
                self._running = False
                if hasattr(events, "_set_running_loop"):
                    events._set_running_loop(None)
Beispiel #16
0
    def run_forever_37(self):
        # from Python 3.7 asyncio.base_events
        self._check_closed()
        old_thread_id = self._thread_id
        old_running_loop = events._get_running_loop()
        self._set_coroutine_origin_tracking(self._debug)
        self._thread_id = threading.get_ident()

        old_agen_hooks = sys.get_asyncgen_hooks()
        sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                               finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = old_thread_id
            events._set_running_loop(old_running_loop)
            self._set_coroutine_origin_tracking(False)
            sys.set_asyncgen_hooks(*old_agen_hooks)
Beispiel #17
0
    def post(self, request):

        form = ModelForm(request.POST)
        if form.is_valid():
            print("It always goes from here")
            range_from = form.cleaned_data['range_from']
            range_to = form.cleaned_data['range_to']
            choosing_selling_sites = form.cleaned_data['choosing_selling_sites']
            print(choosing_selling_sites)
            if 'Flipkart' in choosing_selling_sites:
                furl_for_multi_page = []

                async def data_render(data):
                    with ThreadPoolExecutor(max_workers=10) as executor:
                        with requests.session() as session:
                            loop = asyncio.get_event_loop()
                            tasks = [
                                loop.run_in_executor(executor, Page_render,
                                                     *(data_from_fun, page, range_from, range_to, session)) for
                                data_from_fun in data
                            ]
                            for response in await asyncio.gather(*tasks):
                                furl_for_multi_page.append(response)

                    return furl_for_multi_page



                if events._get_running_loop() is not None:
                    raise RuntimeError(
                        "asyncio.run() cannot be called from a running event loop")

                # if not coroutines.iscoroutine(main):
                #     raise ValueError("a coroutine was expected, got {!r}".format(main))

                loop = events.new_event_loop()
                try:
                    events.set_event_loop(loop)
                    loop.set_debug(debug)
                    loop.run_until_complete(data_render(data))
                finally:
                    try:
                        # _cancel_all_tasks(loop)
                        loop.run_until_complete(loop.shutdown_asyncgens())
                    finally:
                        events.set_event_loop(None)
                        loop.close()
                furl_for_multi_page = furl_for_multi_page[0]
                print("length of flipkart", len(furl_for_multi_page))
            else:
                furl_for_multi_page = []
                print("NOT FOR FLIPKART", furl_for_multi_page)

            if 'Amazon' in choosing_selling_sites:
                amazon_url = amazon_page_randor(range_from, range_to, page)
                print("length of amazon", len(amazon_url))
            else:
                amazon_url = []

                print("NOT FOR AMAZON", amazon_url)

            sleep(5)
            return render(request, 'url_table.html', {'furl_for_multi_page': furl_for_multi_page, 'aurl_for_multi_page': amazon_url, 'range_from': range_from, 'range_to':range_to})
 def _get_event_loop(stacklevel=3):
     loop = events._get_running_loop()
     if loop is None:
         loop = events.get_event_loop_policy().get_event_loop()
     return loop