2016-08-11 3 views
0

여러 프로세스를 시작하는 2 개의 스크립트가 있습니다. 지금은 두 개의 다른 터미널을 열고 Python을 실행하여 두 스크립트를 모두 시작합니다. 하나의 명령 또는 하나의 스크립트를 실행하는 방법으로 어떻게이 작업을 수행 할 수 있습니까?다중 처리 ... 2 터미널에서 2 스크립트 실행

Start.py 1

# globals 
my_queue = multiprocessing.Manager().Queue() # queue to store our values 
stop_event = multiprocessing.Event() # flag which signals processes to stop 
my_pool = None 

def my_function(foo): 
    print("starting %s" % foo) 
    try: 
     addnews.varfoo) 
    except Exception,e: 
     print str(e) 

MAX_PROCESSES = 50 
my_pool = multiprocessing.Pool(MAX_PROCESSES) 

x = Var.objects.order_by('name').values('link') 
for t in x: 
    t = t.values()[0] 
    my_pool.apply_async(my_function, args=(t,)) 
my_pool.close() 
my_pool.join() 

Start1.py 2

# globals 
MAX_PROCESSES = 50 
my_queue = multiprocessing.Manager().Queue() # queue to store our values 
stop_event = multiprocessing.Event() # flag which signals processes to stop 
my_pool = None 

def my_function(var): 
    var.run_main(var) 
    stop_event.set() 


def var_scanner(): 
    # Since `t` could have unlimited size we'll put all `t` value in queue 
    while not stop_event.is_set(): # forever scan `values` for new items 
    y = Var.objects.order_by('foo).values('foo__foo') 
    for t in y: 
     t = t.values()[0] 
     my_queue.put(t) 

try: 
    var_scanner_process = multiprocessing.Process(target=var_scanner) 
    var_scanner_process.start() 
    my_pool = multiprocessing.Pool(MAX_PROCESSES) 

    #while not stop_event.is_set(): 
    try: # if queue isn't empty, get value from queue and create new process 
     var = my_queue.get_nowait() # getting value from queue 
     p = multiprocessing.Process(target=my_function, args=(var,)) 
     p.start() 
    except Queue.Empty: 
     print "No more items in queue" 
     time.sleep(1) 
     #stop_event.set() 

except KeyboardInterrupt as stop_test_exception: 
    print(" CTRL+C pressed. Stopping test....") 
    stop_event.set() 
+2

참고 구문 강조, 당신은 스크립트 2 – Barmar

+0

에서 닫히지 않은 인용문이? – Barmar

+0

'xterm -e 'python start.py'를 사용하여 스크립트를 실행하는 새 창을 열 수 있습니다. 그게 당신이 찾고있는거야? – Barmar

답변

1

당신은 & 쉘 수정을 사용하여 동일한 터미널에 백그라운드에서 첫 번째 스크립트를 실행할 수 있습니다. 두 스크립트가`Start.py` 이름이 같은 이유는 무엇입니까

python start.py & 
python start1.py 
+0

감사합니다. 이 둘은 다른 부모의 다중 처리 스크립트로 시작했지만 작동하지는 않았습니다. – Dorian