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


Python Helper.advertise方法代码示例

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


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

示例1: Server

# 需要导入模块: from helper import Helper [as 别名]
# 或者: from helper.Helper import advertise [as 别名]
class Server(object):
    def __init__(self, name, port=None, use_bonjour=False, data_callback=None, validate_result=None):
        '''note that data_callback must be thread safe'''
        self.port = port or 8139
        self.jobnum = 0
        self.queue = deque()
        self.completed = {}
        self.lock = threading.Lock()
        self.use_bonjour = use_bonjour
        self.data_callback = data_callback
        self.validate_result = validate_result
        self.serving = False
        self.helper = Helper(self, self.port)

    def add_work(self, work_blob):
        with self.lock:
            self.queue.append((self.jobnum, work_blob))
            this_job = self.jobnum
            self.jobnum += 1
        return this_job

    def fetch_result(self, jobnum=None):
        with self.lock:
            if jobnum is None:
                if len(self.completed):
                    jobnum = self.completed.keys()[0]
                else:
                    return None
            if jobnum in self.completed:
                return self.completed.pop(jobnum)
            return None

    def next_job(self):
        with self.lock:
            if len(self.queue) > 0:
                jobnum, work_blob = self.queue[0]
                self.queue.rotate(-1)
                return "%d %s"%(jobnum, work_blob)
            else:
                return "NOWORK"

    def receive_work(self, jobnum, result_dict):
        # XXX - this should probably cache results in files, to prevent overload
        # XXX - need to check work is valid before removing from work queue
        if self.validate_result is not None:
            if not self.validate_result(result_dict):
                sys.stderr.write("NUAGEUX: job %s not accepted\n"%(jobnum))
                return
        with self.lock:
            # only accept the first answer, though we could do some consistency checks here.
            if jobnum not in self.completed:
                self.completed[jobnum] = result_dict
                # This seems the easiest way to remove from the queue given only the jobnum
                # (I guess we could add a self.jobnum_to_work dict, but would it be cleaner?)
                for idx in range(len(self.queue)):
                    if self.queue[0][0] == jobnum:
                        self.queue.popleft()
                    self.queue.rotate(1)

    def start(self):
        self.helper.start(self.port)

    def stop(self):
        self.helper.stop()

    def clear(self):
        with self.lock:
            self.queue.clear()
            self.completed = {}

    def base_URL(self):
        return 'http://%s:%d'%(socket.getfqdn(), self.port)

    def advertise(self, name, protocol):
        assert protocol[0].isalpha() and (len(protocol) < 16) and protocol.replace('-', '').isalpha(), "protocol must be alphanumeric+dashes, <= 15 characters, and start with alphanumeric"
        self.helper.advertise(name, protocol)
开发者ID:robertRalston,项目名称:nuageux,代码行数:78,代码来源:server.py


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