2016-01-24 1 views
-1

Я бегу 2 питона сценариев, скажем, main.py и test.py
В main.py я уверен, выполнение get_details функции «х» количество раз через каждые 30 секунд.вызов функции х число раз и выполнить его из другого сценария

ПРИМЕЧАНИЕ: Я хочу выполнить funA, funcB funC в последовательности. Проблема, с которой я столкнулся, - когда я запускаю test.py, он сначала запускает funcC(), хотя я сначала вызываю funcA().

test.py

def funcA(): 
    #do something 
    funcB() 

def funcB(): 
    #do something 
    funcC() 

def funcC(): 
    #here i want to execute script main.py 
    #My attempt 1 : 
    import subprocess 
    import sys 
    theproc = subprocess.Popen([sys.executable, "main.py"]) 
    theproc.communicate() 

    #------OR----------- 

    #My attempt 2: 
    execfile("main.py") 

main.py

import threading 
def get_details(a,b,c): 
    #do something ...   


class RepeatEvery(threading.Thread): 
    def __init__(self, interval, func, *args, **kwargs): 
     threading.Thread.__init__(self) 
     self.interval = interval # seconds between calls 
     self.func = func   # function to call 
     self.args = args   # optional positional argument(s) for call 
     self.kwargs = kwargs  # optional keyword argument(s) for call 
     self.runable = True 
    def run(self): 
     while self.runable: 
      self.func(*self.args, **self.kwargs) 
      time.sleep(self.interval) 
    def stop(self): 
     self.runable = False 

thread = RepeatEvery(30, get_details,"arg1","arg2","arg3") 
print "starting" 
thread.start() 
thread.join(21) # allow thread to execute a while... 

Я хочу, чтобы выполнить скрипт main.py только после того, как все функции (funcA, funcB) выполнено должным образом. Но в моем случае main.py выполняется сначала, а затем управление возвращается к test.py, и он выполняет funcA() и funcB().

Что мне здесь не хватает?

ответ

0

Хорошо. Я переписал ваш код, чтобы он работал, как вы сказали.

main.py ...

#Good design for small classes: keep global functions separate for people who want 
#to explore the type, but not everything that comes along with it. 
#I moved the the global functions and code execution from top and bottom to test.py 

import threading 
import time #You forgot to import time. 

class RepeatEvery(threading.Thread): 
    def __init__(self, interval, func, *args, **kwargs): 
     threading.Thread.__init__(self) 
     self.interval = interval # seconds between calls 
     self.func = func   # function to call 
     self.args = args   # optional positional argument(s) for call 
     self.kwargs = kwargs  # optional keyword argument(s) for call 
     self.runable = True 
    def run(self): 
     while self.runable: 
      self.func(*self.args, **self.kwargs) 
      time.sleep(self.interval) 
    def stop(self): 
     self.runable = False 

    """ We couuuld have done this, but why bother? It is hard to work with. 
    def get_details(self,a,b,c): 
    #do something else as a function of the class... 
    """ 

test.py ...

import main #File where class lives. 

def funcA(): 
    #do something 
    print ("In A...") #Helps us observe scope. 
    funcB() 

def funcB(): 
    #do something 
    print("In B...") #scope 
    funcC() 

def funcC(): 
    #here i want to execute script main.py 
    #My attempt 1 : 
    print("In C...") #scope 
    main() #Reached C, lets run main now... 

    #This is one way to do allow a function to be accessible to your class. 
def get_details(a,b,c): 
    #do something else as a function of ¬[class] test.py operating on 
    #a RepeatEvery object... 
    pass 

def main(): #Function main is separate from class main. It houses our opening code. 
    thread = main.RepeatEvery(30, get_details,"arg1","arg2","arg3") 
    print ("starting") 
    thread.start() 
    thread.join(21) # allow thread to execute a while... 

funcA() 
Смежные вопросы