Пример #1
0
def parallel_evaluate(expressions, evaluator_spec=None, max_evaluators=4, loop=None):
    """ Start a kernel pool and evaluate the expressions in parallel. 
    
    The pool is created with the value of `evaluator_spec`. The pool is automatically stopped when it is no longer
    needed. The expressions are evaluated and returned in order.

    Note that each evaluation should be independent and not rely on any previous one. There is no guarantee that two
    given expressions evaluate on the same kernel.
    """
    loop = loop or asyncio.get_event_loop()
    pool = None
    try:
        pool = WolframEvaluatorPool(evaluator_spec, poolsize=max_evaluators, loop=loop)
        loop.run_until_complete(pool.start())
        return pool.evaluate_all(expressions)
    finally:
        if pool:
            loop.run_until_complete(pool.terminate())
    def handle(self, domain, port, path, kernel, poolsize, lazy, index, demo,
               initfile, startuptimeout, client_max_size, **opts):

        if demo is None or demo:
            path = self.demo_path(self.demo_choices[demo])

        path = os.path.abspath(os.path.expanduser(path))

        client_max_size = int(client_max_size * (1024**2))

        try:
            session = create_session(kernel,
                                     poolsize=poolsize,
                                     initfile=initfile,
                                     STARTUP_TIMEOUT=startuptimeout)

        except WolframKernelException as e:
            self.print(e)
            self.print("Use --help to display all available options.")
            sys.exit(1)

        loop = asyncio.get_event_loop()

        async def main():

            view = create_view(session, path, index=index, **opts)

            def request_factory(*args, **opts):
                return web.BaseRequest(*args,
                                       **opts,
                                       client_max_size=client_max_size,
                                       loop=loop)

            runner = self.ServerRunner(
                self.Server(view,
                            access_log_class=self.AccessLogger,
                            request_factory=request_factory))
            await runner.setup()
            await self.TCPSite(runner, domain, port).start()

            self.print_separator()

            isdir = os.path.isdir(path)

            for args in (
                ("Address", "http://%s:%s/" % (domain, port)),
                (isdir and "Folder" or "File", path),
            ):
                self.print_line(*args)

            if isdir:

                if index:
                    self.print_line("Index", index)

                if not os.path.exists(os.path.join(path, index)):
                    self.print_separator()
                    self.print_line(
                        "Warning",
                        "The folder %s doesn't contain an %s file." %
                        (path, index))
                    self.print_line(
                        "", "No content will be served for the homepage.")

            self.print_separator()

            if sys.platform == "win32":
                self.print_line("(Press CTRL+BREAK to quit)")
            else:
                self.print_line("(Press CTRL+C to quit)")

            self.print_line()

            logging.basicConfig(level=logging.INFO, format="%(message)s")

            if not lazy:
                await session.start()

            while True:
                await asyncio.sleep(3600)

        try:
            loop.run_until_complete(main())
        except KeyboardInterrupt:
            if session.started:
                self.print("Requested server shutdown, closing session...")
                loop.run_until_complete(session.stop())

        loop.close()