def rtask_proc(task=None):
import os
# receive object from client_proc task
cobj = yield task.receive()
if not cobj:
raise StopIteration
# Input file is already copied at where this rtask is running (by client).
# For given input file, create an output file with each line in the output
# file computed as length of corresponding line in input file
cobj.result_file = 'result-%s' % cobj.data_file
with open(cobj.data_file, 'r') as data_fd:
with open(cobj.result_file, 'w') as result_fd:
for lineno, line in enumerate(data_fd, start=1):
result_fd.write('%d: %d\n' % (lineno, len(line)-1))
# 'sleep' to simulate computing
yield task.sleep(cobj.n)
# transfer the result file to client
status = yield pycos.Pycos().send_file(cobj.client.location, cobj.result_file,
overwrite=True, timeout=30)
if status:
print('Could not send %s to %s' % (cobj.result_file, cobj.client.location))
cobj.result_file = None
cobj.client.send(cobj)
os.remove(cobj.data_file)
os.remove(cobj.result_file)
# this generator function is used to create local task (at the client) to
# communicate with a remote task
评论列表
文章目录