How execute at least two python files at once when imported?
Christian Gollwitzer
auriocus at gmx.de
Wed Nov 6 03:05:27 EST 2019
Am 06.11.19 um 03:59 schrieb Dennis Lee Bieber:
> On Tue, 5 Nov 2019 10:33:20 -0800 (PST), Spencer Du
> <spencerdu at hotmail.co.uk> declaimed the following:
>
>> Hi
>>
>> I want to execute at least two python files at once when imported but I dont know how to do this. Currently I can only import each file one after another but what i want is each file to be imported at the same time. Can you help me write the code for this? embedded.py is the main file to execute.
>
>
> Short answer: you don't.
>
> When you import a module, the code for that module is parsed and
> anything that is module level executable statement is done (note: "def" is
> an executable statement -- it creates a function object contained the
> parsed body and binds it to the provided name). When the parser gets to the
> end of the module, it returns to the parent level and the next statement is
> executed.
>
> Unless you use 1) threads; 2) subprocesses; or 3) multiprocess a Python
> program only has one line of control, and that control is sequential.
Since some of these example programs use asyncio, there is a 4th method.
You convert all the programs to use asyncio, remove the event loop from
these programs, i.e. remove the
asyncio.get_event_loop().run_until_complete(main())
from the individual programs, and then you run a single event loop in
your main program. Something like
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(laserembeded.main(),
camerasembedded.main()))
Christian
More information about the Python-list
mailing list