How execute at least two python files at once when imported?

Spencer Du spencerdu at hotmail.co.uk
Wed Nov 6 04:43:15 EST 2019


On Wednesday, 6 November 2019 09:05:42 UTC+1, Christian Gollwitzer  wrote:
> 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

Ok I am interested in knowing how i can do it via either 1) threads; 2) subprocesses; or 3) multiprocess; depending on what you think is the best method.

Thanks


More information about the Python-list mailing list