def single_thread_main(): if not os.path.isfile("/Users/eddie/PycharmProjects/amaproj/uk.xls"): messagebox.showerror("error", u"请将uk.xls放到和amazon.exe相同目录下") exit(0) products = [] rb = xlrd.open_workbook("/Users/eddie/PycharmProjects/amaproj/uk.xls") try: sheet = rb.sheet_by_name("asin") count = sheet.nrows for i in range(0, count): print(sheet.cell_value(i, 0)) products.append(sheet.cell_value(i, 0)) except: messagebox.showerror("error", u"uk.xls中必须包含名字为asin的sheet") exit(0) wb = copy(rb) sheet = wb.add_sheet( time.strftime(u"%m-%d_%H-%M", time.localtime(time.time()))) #写头部标题: tableheaders = GlobalTools.get_table_header() row = 0 col = 0 for item in tableheaders: sheet.write(row, col, item) col += 1 currrow = 1 from queue import Queue for product in products: amazonobj = amazon(Queue(), product, "uk") amazonobj.prerequest() result = amazonobj.parse(sheet, currrow) currrow += 1 try: wb.save("/Users/eddie/PycharmProjects/amaproj/uk.xls") except: messagebox.showerror("error", u"保存文件失败,运行时,请不要打开uk.xls文件")
def main(queue, countrycode): if not os.path.isfile(GlobalTools.getExcelFile(countrycode)): # messagebox.showerror("error", u"请将uk.xls放到和amazon.exe相同目录下") queue.put(u"ERROR:请将" + countrycode + u".xls放到和amazon.exe相同目录下") exit(0) products = [] rb = xlrd.open_workbook(GlobalTools.getExcelFile(countrycode)) try: sheet = rb.sheet_by_name("asin") count = sheet.nrows for i in range(0, count): print(sheet.cell_value(i, 0)) products.append(sheet.cell_value(i, 0)) except: # messagebox.showerror("error", u"uk.xls中必须包含名字为asin的sheet") queue.put(u"ERROR:请将" + countrycode + u".xls放到和amazon.exe相同目录下") exit(0) print("copy") wb = copy(rb) sheet = wb.add_sheet( time.strftime(u"%m-%d_%H-%M", time.localtime(time.time()))) # 写头部标题: tableheaders = GlobalTools.get_table_header() row = 0 col = 0 for item in tableheaders: sheet.write(row, col, item) col += 1 pool = multiprocessing.Pool(processes=5) currrow = 1 results = [] for product in products: results.append( pool.apply_async(fun, ( queue, product, countrycode, currrow, sheet, ))) currrow += 1 pool.close() pool.join() # for res in results: # # try: # print res.get() # # except: # # pass for result in results: try: row = result.get() currrow = row[0] print("currrow:" + str(currrow)) col = 0 for i in range(1, len(row)): sheet.write(currrow, col, row[i]) col += 1 except: pass try: wb.save(GlobalTools.getExcelFile(countrycode)) except: queue.put(u"ERROR:保存文件失败,运行时,请不要打开站点对应的xls文件") exit(0) queue.put("finish.")