当前位置: 首页>>代码示例>>Python>>正文


Python BaseManager.get_work_queue方法代码示例

本文整理汇总了Python中multiprocessing.managers.BaseManager.get_work_queue方法的典型用法代码示例。如果您正苦于以下问题:Python BaseManager.get_work_queue方法的具体用法?Python BaseManager.get_work_queue怎么用?Python BaseManager.get_work_queue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在multiprocessing.managers.BaseManager的用法示例。


在下文中一共展示了BaseManager.get_work_queue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RequestGenerator

# 需要导入模块: from multiprocessing.managers import BaseManager [as 别名]
# 或者: from multiprocessing.managers.BaseManager import get_work_queue [as 别名]
class RequestGenerator():
    def __init__(self, host=None, port=None, authkey=None, baseurl=None, name='1', wq=None, rq=None, delinprod=True):
        self.name = name
        self.baseurl = baseurl
        self.delinprod = delinprod
        self.m = None
        if wq is None or rq is None:
            print 'Initializing RequestGenerator: ' + self.name + ' as BaseManager(address=(' + host + ', ' + str(port) + ', authkey=' + authkey + ') with remote queues'
            BaseManager.register('get_work_queue')
            BaseManager.register('get_result_queue')
            self.m = BaseManager(address=(host, port), authkey=authkey)
            self.m.connect()
            self.work_queue = self.m.get_work_queue()
            self.result_queue = self.m.get_result_queue()
        else:
            print 'Initializing RequestGenerator: ' + self.name + ' with shared local queues'
            self.work_queue = wq
            self.result_queue = rq
        #self.work_queue.cancel_join_thread()
        #self.result_queue.cancel_join_thread()
        self.http = urllib3.PoolManager()

    def run(self):
        self.running = True
        print 'Running RequestGenerator: ' + self.name
        while self.running:
            try:
                query, oqt = self.work_queue.get()
                try:
                    ts, taken, qt, nf, sz = self.request_url(query, self.baseurl)
                except ValueError:
                    continue
                self.result_queue.put((ts, taken, qt, nf, sz, oqt))
            except KeyboardInterrupt:
                print '\nKeyboardInterrupt detected in RequestGenerator: ' + self.name + ', attempting to exit...'
                #sys.exc_clear()
                self.stop()
                return

    def stop(self):
        self.running = False
        #self.work_queue.close()
        #self.result_queue.close()
        #if self.m is not None:
        #    self.m.shutdown()


    def request_url(self, query, baseurl):
        #myreq = urllib2.Request(baseurl)
        #myreq.add_data(query)
        req_start = datetime.now()
        ts = time.time()
        #with contextlib.closing(urllib2.urlopen(myreq)) as c:
        #    r = json.load(c)
        r = self.http.request('GET', baseurl + query)
        if r.status != 200:
            print 'Received status: {0} for request: {1}\n{2}'.format(r.status, query, r.data)
            raise ValueError
        d = r.data
        td = datetime.now() - req_start
        taken = int(round((td.microseconds + (td.seconds * 1000000.0)) / 1000))
        sz = len(str(d))
        qt = -1
        nf = -1
        try:
            j = json.loads(d)
            #qt = r[r.find('QTime':') + 7:r.find('}')] #attempt to parse QTime from response
            qt = j.get('responseHeader', {}).get('QTime', -1)
            nf = j.get('response', {}).get('numFound', -1)
        except ValueError:
            print 'ValueError raised attempting to parse json: ' + repr(d)
        return ts, taken, qt, nf, sz
开发者ID:daubman,项目名称:solr-replay,代码行数:74,代码来源:RequestGenerator.py


注:本文中的multiprocessing.managers.BaseManager.get_work_queue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。