多处理...在2个终端上运行2个脚本

时间:2016-08-11 19:28:49

标签: python multiprocessing

我有两个启动多个进程的脚本。现在我打开两个不同的终端并运行python start.py来启动这两个脚本。如何使用一个命令或一个运行一个脚本来实现此目的。

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()

1 个答案:

答案 0 :(得分:1)

您可以使用& shell修饰符在同一终端上的后台运行第一个脚本。

python start.py &
python start1.py