0

im new in python and i want to run two simultaneos programs in python, one will always change the value of X and the other must read this variable and if X change do something, i tried doing this:

first program:

first.py

import time
rocket =0
while rocket<10:
time.sleep(1)
rocket+=1

second.py

import first 
while (first.rocket<10):
   print 'rocket less than 10'
print 'done'

but my results are not what i want because the after the line import first the value of rocket is allways 10 so my second.py never print 'rocket less than 10' only print 'done', how i can solve this. those must be progrmas that runs at same time.

0

3 Answers 3

1

in short: this is not possible.

Long: this is possible by using another techniques as described in this thread:

how-to-share-variables-across-scripts-in-python

0

When you import a file, everything in it will run before proceeding. So what you are doing right now is not actually running two programs in parallel, your import statement in second.py instruct to run first.py, then proceed with the rest of the instruction.

The pythonic way to do it would simply be to pass an object through the import statement. We could define a rocket class that has a charge attribute, the second program checking this attribute and then adding to it through a method. It would look like this.

first.py

import time
class Rocket: # class object usually start with a capital letter.

     def __init__(self): # Method used to initialize an instance of the class
         self.charge = 0

     def add_charge(self):
         self.charge += 1

second.py

import first
rocket = first.Rocket() # We actually create a new object here
while rocket.charge < 10:
    rocket.add_charge()
    print('rocket less than 10')
print 'done'

Note that there is still not two programs running at the same time. The first file defines an object that is then used in the second file. To put it very simply, the first file draws the plan of the object we would be using, the second builds it.

If you'd want to have actually two python scripts running in parallel, they would each be enclosed in their own virtual machine and you'd have to resort to writing to a file with first.py and read from it with second.py but I don't really see the point of running two files separately.

After writing this, and your question again, it seems that I might have assumed you already knew about classes and such, which might not be the case... So feel free to ask away !

1
  • hi, well i have to make a program that should read Serial Data, at 57600 baud rate and then when get the right data(i mean not allways all what i recive is what i want)do some specific process like adds, Multiplications,so the problem is if all of that i do in the same program my math process must take less than 1 ms because if not i will have bytes in the serial bufferand it could fill out, so i thought to make 2 programs one to validate the serial data and pass the right data to the other to do the math while the first is still validating the serial data that recibe. May 23, 2017 at 17:39
-3

Sorry python doesn't work like this. Because whenever you will call first.py, second.py will wait for first.py to finish.

For situations like this, for you will need to build API. Good news is, you can do that with python. Please google about it or check YouTube videos. There is a lot you can do with python.

Not the answer you're looking for? Browse other questions tagged or ask your own question.