加载文件内容后,使齿轮工保持侦听模式

  • 本文关键字:模式 文件 加载 python gearman
  • 更新时间 :
  • 英文 :


我想将一些文件内容加载到内存中,以便可以立即查询。

在齿轮工中,我正在加载文件并将其置于侦听模式。使用 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)
self.input_text = self.input_text.split('n')

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

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

相关内容

  • 没有找到相关文章

最新更新