加载文件内容后,让gearman worker保持在监听模式

时间:2015-02-03 14:38:06

标签: python gearman

我希望将一些文件内容保存在内存中,以便可以立即查询它。

在gearman worker中,我正在加载文件并将其置于聆听模式。在使用gearman客户端发出请求时,worker仅返回加载的内容一次,下次客户端收到None

工人:

class GetLexiconFiles(object):

    def __init__(self):
        self.gm_worker = gearman.GearmanWorker(['localhost:4730'])
        self.loadFiles()
        self.gm_worker.register_task('load_db', self.task_listener_reverse)
        #self.loadFiles()
        #self.gm_worker.work()



    def task_listener_reverse(self, gearman_worker, gearman_job):
        k=float('inf')
        #print "Started loading file"
        self.input_text = self.input_text.split('\n')
        print "Loading completed"
        lexicon = defaultdict(list)
        for i, line in enumerate(self.input_text):
        #print "line is : ", line
            if i >= k: break
            #if i % 100000 == 0: print >>sys.stderr, i
            try:
                if line != '':
                    nl, dbs = line.split(' ', 1)
                    nl = int(nl)
                    dbs = self.str2obj(dbs)
                    lexicon[nl].append(dbs)
                else:
                    pass
            except:
                print >>sys.stderr, 'could not parse line %r' % line
                print traceback.format_exc()
                continue
    return json.dumps(lexicon)


if __name__ == '__main__':
    GetLexiconFiles().gm_worker.work()

客户:

def check_request_status(job_request):
    if job_request.complete:
        #data = json.loads(job_request.result)
        print "Job %s finished!  Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
    elif job_request.timed_out:
        print "Job %s timed out!"
    elif job_request.state == JOB_UNKNOWN:
        print "Job %s connection failed!"

gm_client = gearman.GearmanClient(['localhost:4730'])
tasks = [{'task': 'load_lexicon', 'data': 'This is testing sentence'}, {'task': 'load_db', 'data': 'This is db testing'}]

submitted_requests = gm_client.submit_multiple_jobs(tasks, background=False, wait_until_complete=False)

completed_requests = gm_client.wait_until_jobs_completed(submitted_requests)
print completed_requests[1].result
for completed_job_request in completed_requests:
    check_request_status(completed_job_request)

1 个答案:

答案 0 :(得分:1)

self.input_text = self.input_text.split('\n')

使用这行代码,您将字符串转换为字符串列表。

由于您在下次调用该函数时将结果保存回 self.input_text self.input_text 已经是一个列表,它会引发异常