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


Python Queue.task_done方法代码示例

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


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

示例1: listener

# 需要导入模块: from multiprocessing import Queue [as 别名]
# 或者: from multiprocessing.Queue import task_done [as 别名]
def listener(file : str, workQueue : Queue):
    try:
        f = open(file, 'w', encoding='utf-8')
        f.write("URL|IS_HTTPS|HTTP_HEADER|RESULT\n")
        while 1:
            m = workQueue.get()
            workQueue.task_done()
            if m == 'kill':
                break
            f.write(str(m) + '\n')
            f.flush()
    except BaseException:
        print(sys.exc_info())
        raise
    finally:
        f.close()
开发者ID:ttemrekisa,项目名称:securityheaderchecker,代码行数:18,代码来源:HTTPHeaderCheck.py

示例2: _WorkerClass

# 需要导入模块: from multiprocessing import Queue [as 别名]
# 或者: from multiprocessing.Queue import task_done [as 别名]
class _WorkerClass(object):
    _use_processes = True
    
    def __init__(self, *args, **kwargs):
        if 'use_processes' in kwargs:
            self._use_processes = kwargs['use_processes']
            del kwargs['use_processes']
        
    def _setup_main_queue(self):
        self._queue = Queue()
        
        if self._use_processes is True:
            t = Process(target=self._process_queue)
        else:
            t = _Thread(target=self._process_queue)
        t.name = "Queue"
        t.daemon = True
        t.start()
        print "Queue started", t
    
    def _process_queue(self):
        while 1:
            print "Processing queue... Size is", self._queue.qsize()
            request = self._queue.get()
            print "Request is:",request
            self._do_request(request)
            self._queue.task_done()
    
    def _do_request(self, function, *args, **kwargs):
        if type(function) == type(()):
            function, args, kwargs = function[:]
        if 'callback' in kwargs:
            callback = kwargs['callback']
            del kwargs['callback']
        else:
            callback = None

        if 'error' in kwargs:
            error = kwargs['error']
            del kwargs['error']
        else:
            error = None
        
        print "Run", function, args, kwargs
        try:
            r = function(*args, **kwargs)
            if not isinstance(r, tuple): r = (r,)
            if callback: self._do_callback(callback, *r)
        except Exception as e:
            print "Error occured"
            if error:
                self._do_callback(error, e)
            else:
                print "There was an error running the threaded function:", e
                print _print_exception_details()
    
    def _start_request_in_new_thread(self, function, *args, **kwargs):
        if self._use_processes is True:
            t = Process(target=self.__do_request, args=(function,)+args, kwargs=kwargs)
        else:
            t = _Thread(target=self.__do_request, args=(function,)+args, kwargs=kwargs)
        t.daemon = True
        t.start()
    
    def _add_request_to_main_queue(self, function, *args, **kwargs):
        try:
            getattr(self, '_queue')
        except AttributeError:
            self._setup_main_queue()

        self._queue.put((function, args, kwargs))
        print "Request added to queue. Size is now", self._queue.qsize()
    
    def _do_callback(self, callback, *args):
        def _callback(callback, args):
            callback(*args)
            return False

        _callback(callback, args)
开发者ID:Cybolic,项目名称:vineyard,代码行数:81,代码来源:worker.py

示例3: __init__

# 需要导入模块: from multiprocessing import Queue [as 别名]
# 或者: from multiprocessing.Queue import task_done [as 别名]
class Mailbox:
    """
    Provides messaging functionality for a paxos system instance.
    """

    def __init__(self, config):
        self.config = config
        self.funnel = Queue()
        self.inbox = [Queue() for i in range(config.num_processes)]
        self.message_count = 0

        # Two flags, active to signal when we haven't received any messages
        # for timeout_interval seconds, and terminate to signal when we have
        # been asked to shutdown by the System.
        self.active = True
        self.terminate = False
        # If don't receive any messages in this amount of time, then shutdown.
        self.timeout_interval = 3 * self.config.message_timeout
        # Time stamp of the last seen message, used together with
        # timeout_interval to determine when the mailbox should shutdown.
        self.last_seen = None

    def run(self):
        print("Mailbox started")
        while True:
            if not self.active and self.terminate:
                break
            if self.active and self.last_seen and (time.time() - self.last_seen) > self.timeout_interval:
                self.active = False
            # Take messages off the funnel and deliver them to the appropriate
            # process.
            try:
                dest, msg = self.funnel.get(timeout=0.5)
                self.last_seen = time.time()
            except queue.Empty:
                pass
            else:
                self.inbox[dest].put(msg)
        print("Mailbox shutting down")

    def send(self, to, msg):
        """
        Send msg to process id ``to``.
        """
        # Funnel all messages through a primary queue so that we can keep track
        # of when we are done (i.e. all messages are processed).
        self.message_count += 1
        self.funnel.put((to, msg))

    def recv(self, from_):
        """
        Receive (blocking) msg destined for process id ``from_``.
        """
        return self.inbox[from_].get()

    def task_done(self, pid):
        """
        Inform pid's queue that it has processed a task.
        Called by agents when they are done processing a message, and used by
        the queue instance to know when all messages have been processed.
        """
        self.funnel.task_done()

    def join(self):
        """
        Block until all messages have finished processing and we haven't had
        any messages for a while (i.e. active set to False).
        """
        while self.active:
            time.sleep(0.5)
        # Don't join funnel queue because there's a good chance that it will
        # never be fully exhausted due to heart beat messages.
        #self.funnel.join()

    def shutdown(self):
        """
        Perform any shutdown actions prior to quitting.  In this base Mailbox
        class this is just a hook that isn't used.
        """
        pass

    def quit(self):
        self.terminate = True
开发者ID:M1lan,项目名称:python-paxos,代码行数:85,代码来源:sim.py


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