我正在尝试处理以下任务:
`
def read(fin):
for query in fin:
return query
def search(query):
# Send the query to something like Google Search
result = google(query)
return result
def write(fout, result):
fout.write(result)
`
我参考Gevent教程,但无法弄清楚如何将这三个函数放入gevent。
答案 0 :(得分:1)
我不知道你的例子与gevent有关。无论如何,它仍然可以通过下面的示例代码进行处理。
import gevent
def read(fin):
for query in fin:
return query
def search(query):
# Send the query to something like Google Search
result = google(query)
return result
def write(fout, result):
fout.write(result)
# read from different files
input_files = ["input1.json", "input2.json" ... ]
# write result into output file
fout = open("output.json", 'wb')
# get queries from input files
queries = []
for input_file in input_files:
with open(input_file, 'rb') as fin:
queries.append(gevent.joinall([gevent.spawn(read, fin)]))
# store results of queries
results = []
for item in [query for query in queries]:
results.append(gevent.joinall([gevent.spawn(search, item[0].value)]))
# save results into output file
for item in [result for result in results]:
gevent.joinall([gevent.spawn(write, fout, item[0].value)])
事物是读取搜索和搜索到写入都有依赖,它不能使用异步方式来实现这一点。因此,在该示例中,它只是可以处理每个函数并逐个保存返回。