2016-08-11 53 views
0

我有2個啓動多個進程的腳本。現在我打開兩個不同的終端並運行python start.py來啓動兩個腳本。我如何用一個命令或者一個腳本來實現這一點。多處理...在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

一個未關閉的報價兩個腳本爲什麼有相同的名字'Start.py'? – Barmar

+0

你可以使用'xterm -e'python start.py'打開一個運行腳本的新窗口。那是你在找什麼? – Barmar

回答

1

可以在同一個終端上的後臺運行的第一腳本,使用&殼改性劑。

python start.py & 
python start1.py 
+0

真棒,謝謝。我有這兩個開始與另一個父多處理腳本,但它不工作。 – Dorian

相關問題