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

Richard Damon Richard at Damon-Family.org
Wed Nov 6 22:40:22 EST 2019


On 11/6/19 3:37 PM, Michael Torrie wrote:
> On 11/6/19 9:16 AM, Spencer Du wrote:
>> I just wanted a way to import at least two python files in parallel
>> and I wanted to know how this can be done or a reason why its bad as
>> stated in another post.
> It's not "bad," but it's also not possible.  Nor does it make sense.
> That's why so many people are questioning what you are really trying to
> accomplish.  By definition a Python script is a sequence of statements
> that are read and executed in sequence.  You can't just run them all at
> once, nor would it make sense to do so.
>
> If you want to run multiple python functions in parallel, there are
> threads for that, multiprocessing, and also asynchronous scheduling.
>
> But no one can tell you what to do because none of us have any clue what
> you are trying to accomplish.

I would disagree that it isn't possible, depending on exactly what is
wanted, I can think of several ways to do it.

Simplest option is fork off a separate process and run the second script
from that process, that would work if the two scripts don't need much
communication between themselves, and you would need to manually add
what ever communication was needed.

A second option is to create a second thread, and do one if the imports
in that thread, and one in the main thread. This is likely the closest
to the stated goal, but likely, due to the GIL, slower than doing one
after the other.

-- 
Richard Damon



More information about the Python-list mailing list