On 14 September 2017 at 11:44, Eric Snow <ericsnowcurren...@gmail.com>
wrote:

> Examples
> ========
>
> Run isolated code
> -----------------
>
> ::
>
>    interp = interpreters.create()
>    print('before')
>    interp.run('print("during")')
>    print('after')
>

A few more suggestions for examples:

Running a module:

    main_module = mod_name
    interp.run(f"import runpy; runpy.run_module({main_module!r})")

Running as script (including zip archives & directories):

    main_script = path_name
    interp.run(f"import runpy; runpy.run_path({main_script!r})")

Running in a thread pool executor:

    interps = [interpreters.create() for i in range(5)]
    with concurrent.futures.ThreadPoolExecutor(max_workers=len(interps)) as
pool:
        print('before')
        for interp in interps:
            pool.submit(interp.run, 'print("starting"); print("stopping")'
        print('after')

That last one is prompted by the questions about the benefits of keeping
the notion of an interpreter state distinct from the notion of a main
thread (it allows a single "MainThread" object to be mapped to different OS
level threads at different points in time, which means it's easier to
combine with existing constructs for managing OS level thread pools).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to