0
我一直在使用一個腳本(上圖)並行運行在16個處理器的Ubuntu的服務器某項任務,它的實際工作,但我有一個關於它的幾個問題:這個並行任務代碼如何在Python中工作?
- 什麼代碼實際上做?
- 隨着越來越多的工人設置腳本運行得更快,但工人的限制是什麼?我已經跑了100個。
怎麼可能改進呢?
#!/usr/bin/env python from multiprocessing import Process, Queue from executable import run_model from database import DB import numpy as np def worker(work_queue, db_conection): try: for phone in iter(work_queue.get, 'STOP'): registers_per_number = retrieve_CDRs(phone, db_conection) run_model(np.array(registers_per_number), db_conection) #print("The phone %s was already run" % (phone)) except Exception: pass return True def retrieve_CDRs(phone, db_conection): return db_conection.retrieve_data_by_person(phone) def main(): phone_numbers = np.genfromtxt("../listado.csv", dtype="int")[:2000] workers = 16 work_queue = Queue() processes = [] #print("Process started with %s" % (workers)) for phone in phone_numbers: work_queue.put(phone) #print("Phone %s put at the queue" % (phone)) #print("The queue %s" % (work_queue)) for w in xrange(workers): #print("The worker %s" % (w)) # new conection to data base db_conection = DB() p = Process(target=worker, args=(work_queue, db_conection)) p.start() #print("Process %s started" % (p)) processes.append(p) work_queue.put('STOP') for p in processes: p.join() if __name__ == '__main__': main()
乾杯!