我正在编写一个多线程脚本的开始,该脚本从csv文件读取输入,其中包含我希望读入queue()
的单列数据。这将发生在f(q):
。然后我将从worker函数中的队列中读取并执行一些操作然后输出带有数据的新csv。我被困在哪里,无论我把q.put([urls])
缩进到哪里,我只能设法捕获队列中的单个值()。为什么它不像print urls
语句那样循环遍历URL?任何帮助表示赞赏。提前致谢。
# -*- coding: utf-8 -*-
import requests
import csv
from multiprocessing import Process, Queue, Pool
#read rows in input file into the queue()
def f(q):
with open('test.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
urls = row[0]
print urls
q.put([urls])
#process the queue() and write output
def worker():
# do work
# ...
# here
if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q,))
p.start()
print q.get() # prints urls as queue output debug
p.join()