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


Python LifoQueue.get_nowait方法代码示例

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


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

示例1: LiveviewStreamThread

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
    class LiveviewStreamThread(threading.Thread):
        def __init__(self, url):
            # Direct class call `threading.Thread` instead of `super()` for python2 capability
            threading.Thread.__init__(self)
            self.lv_url = url
            self._lilo_head_pool = LifoQueue()
            self._lilo_jpeg_pool = LifoQueue()

            self.header = None
            self.frameinfo = []

        def run(self):
            sess = urlopen(self.lv_url)

            while True:
                header = sess.read(8)
                ch = common_header(header)

                data = sess.read(128)
                payload = payload_header(data, payload_type=ch['payload_type'])

                if ch['payload_type'] == 1:
                    data_img = sess.read(payload['jpeg_data_size'])
                    assert len(data_img) == payload['jpeg_data_size']

                    self._lilo_head_pool.put(header)
                    self._lilo_jpeg_pool.put(data_img)

                elif ch['payload_type'] == 2:
                    self.frameinfo = []

                    for x in range(payload['frame_count']):
                        data_img = sess.read(payload['frame_size'])
                        self.frameinfo.append(payload_frameinfo(data_img))

                sess.read(payload['padding_size'])

        def get_header(self):
            if not self.header:
                try:
                    self.header = self._lilo_head_pool.get_nowait()
                except Exception as e:
                    self.header = None
            return self.header

        def get_latest_view(self):
            # note this is a blocking call
            data_img = self._lilo_jpeg_pool.get()

            # retrive next header
            try:
                self.header = self._lilo_head_pool.get_nowait()
            except Exception as e:
                self.header = None

            return data_img

        def get_frameinfo(self):
            return self.frameinfo
开发者ID:Bloodevil,项目名称:sony_camera_api,代码行数:61,代码来源:pysony.py

示例2: SaveManager

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
class SaveManager(QObject):

    start_save = pyqtSignal()
    report_error = pyqtSignal(object)
    save_done = pyqtSignal()

    def __init__(self, parent):
        QObject.__init__(self, parent)
        self.count = 0
        self.last_saved = -1
        self.requests = LifoQueue()
        t = Thread(name='save-thread', target=self.run)
        t.daemon = True
        t.start()
        self.status_widget = w = SaveWidget(parent)
        self.start_save.connect(w.start, type=Qt.QueuedConnection)
        self.save_done.connect(w.stop, type=Qt.QueuedConnection)

    def schedule(self, tdir, container):
        self.count += 1
        self.requests.put((self.count, tdir, container))

    def run(self):
        while True:
            x = self.requests.get()
            if x is None:
                self.requests.task_done()
                self.__empty_queue()
                break
            try:
                count, tdir, container = x
                self.process_save(count, tdir, container)
            except:
                import traceback
                traceback.print_exc()
            finally:
                self.requests.task_done()

    def __empty_queue(self):
        ' Only to be used during shutdown '
        while True:
            try:
                self.requests.get_nowait()
            except Empty:
                break
            else:
                self.requests.task_done()

    def process_save(self, count, tdir, container):
        if count <= self.last_saved:
            shutil.rmtree(tdir, ignore_errors=True)
            return
        self.last_saved = count
        self.start_save.emit()
        try:
            self.do_save(tdir, container)
        except:
            import traceback
            self.report_error.emit(traceback.format_exc())
        self.save_done.emit()

    def do_save(self, tdir, container):
        try:
            save_container(container, container.path_to_ebook)
        finally:
            shutil.rmtree(tdir, ignore_errors=True)

    @property
    def has_tasks(self):
        return bool(self.requests.unfinished_tasks)

    def wait(self, timeout=30):
        if timeout is None:
            self.requests.join()
        else:
            try:
                join_with_timeout(self.requests, timeout)
            except RuntimeError:
                return False
        return True

    def shutdown(self):
        self.requests.put(None)
开发者ID:kutanari,项目名称:calibre,代码行数:85,代码来源:save.py

示例3: __init__

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
class DepFetcher:
    def __init__(self, comake):
        self.comake = comake
        self.root = os.getenv('COMAKEPATH')
        if not path.isdir(self.root):
            makedirs(self.root)
        self.queue = Queue()
        self.stack = LifoQueue()
        self.dep_set = set()
        self.thread = None
        self.stop = False
        self.work_num = 4
        self.dep_version = {}

    def set_work_num(self, num):
        self.work_num = num

    def worker(self):
        while True:
            try:
                dep = self.queue.get_nowait()
            except Empty:

                break
            else:
                repo = utils.GetPathFromUri(dep['uri'])
                if repo:
                    self.stack.put(repo)

                if dep["uri"] not in self.dep_set:
                    try:
                        self.dep_version[dep["uri"]] = dep
                        self.dep_set.add(dep["uri"])
                        deps = self.getOneRepo(dep)
                    except Exception as e:
                        print RedIt(e.message)
                    else:
                        for d in deps:
                            if len(d["uri"]) == 0:
                                continue
                            self.queue.put(d)
                self.queue.task_done()

    def getRepo(self):
        for dep in self.comake["dependency"]:
            if len(dep['uri']) == 0:
                continue
            self.queue.put(dep)
        self.thread = Thread(target=self.worker)
        self.thread.start()
        self.thread.join()
        self.queue.join()

        duplicated_dep = set()
        dep_list = []
        while True:
            try:
                dep = self.stack.get_nowait()
            except Empty:
                break
            else:
                if dep not in duplicated_dep:
                    duplicated_dep.add(dep)
                    dep_list.append(dep)

        with open('.comake_deps', "wb") as f:
            pickle.dump(dep_list, f)

    def getOneRepo(self, dep):
        repo = None
        if len(dep["uri"]) == 0:
            return {}
        url = urlparse(dep["uri"])
        if url.scheme == u"file":
            #TODO use local file
            repo_path = path.sep.join([self.root, url.netloc, url.path]);
            local_path = url.path.split(os.path.sep)
            if path.exists(repo_path):
                print GreenIt("[NOTICE] find local repo success: " + repo_path)
            else:
                print RedIt("[ERROR] find local repo failed: " + dep["uri"])
        else:
            local_path = [self.root, url.netloc]
            local_path.extend([x for x in url.path.split('/') if x])
            if not local_path[-1].endswith('.git'):
                print RedIt("[error] wrong dependency uri format: {}".format(dep['uri']))
                return []
            local_path[-1] = local_path[-1][0:-4]
            repo_path = path.sep.join(local_path)

            if not path.isdir(repo_path):
                makedirs(repo_path)
            try:
                repo = Repo(repo_path)
            except git.exc.InvalidGitRepositoryError:
                repo = Repo.clone_from(dep['uri'], repo_path)
            finally:
                try:
                    if dep['tag'] is not "master":
                        tagRepo = repo.tags[dep['tag']]
#.........这里部分代码省略.........
开发者ID:boully,项目名称:comake,代码行数:103,代码来源:GetDep.py

示例4: ThreadPool

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
class ThreadPool(object):
    def __init__(self, threadNum, max_tasks_per_period=10, seconds_per_period=30):
        self.pool = []  # 线程池
        self.threadNum = threadNum  # 线程数
        self.runningLock = Lock()  # 线程锁
        self.taskLock = Lock()  # getTask函数的锁
        self.running = 0  # 正在run的线程数

        # 设置为LIFO队列:在抓取了第一个post的页面后,随后需要添加所有其后的评论页,
        # 使用LIFO队列可以保证尽快将第一个post的所有评论抓取到,并存储
        self.taskQueue = LifoQueue()  # 任务队列

        # 一分钟内允许的最大访问次数
        self.max_tasks_per_period = max_tasks_per_period
        # 定制每分钟含有的秒数
        self.seconds_per_period = seconds_per_period
        # 当前周期内已经访问的网页数量
        self.currentPeriodVisits = 0
        # 将一分钟当作一个访问周期,记录当前周期的开始时间
        self.periodStart = time.time()  # 使用当前时间初始化

    def startThreads(self):
        """Create a certain number of threads and started to run 
        All Workers share the same ThreadPool
        """
        # 开始当前的抓取周期
        self.periodStart = time.time()
        for i in range(self.threadNum):
            self.pool.append(Worker(self, i))

    def stopThreads(self):
        for thread in self.pool:
            thread.stop()
            thread.join()
        del self.pool[:]

    def putTask(self, func, *args, **kargs):
        self.taskQueue.put((func, args, kargs))

    def getTask(self, *args, **kargs):
        # 进行访问控制: 判断当前周期内访问的网页数目是否大于最大数目
        if self.currentPeriodVisits >= self.max_tasks_per_period - 2:
            timeNow = time.time()
            seconds = timeNow - self.periodStart
            if seconds < self.seconds_per_period:  # 如果当前还没有过一分钟,则sleep
                remain = self.seconds_per_period - seconds
                print "ThreadPool Waiting for " + str(remain) + " seconds."
                time.sleep(int(remain + 1))

            self.periodStart = time.time()  # 重新设置开始时间
            self.currentPeriodVisits = 0

        try:
            # task = self.taskQueue.get(*args, **kargs)
            task = self.taskQueue.get_nowait()
        except Empty:
            return (None, None, None)

        self.currentPeriodVisits += 1

        return task

    def taskJoin(self, *args, **kargs):
        """Queue.join: Blocks until all items in the queue have been gotten and processed.
        """
        self.taskQueue.join()

    def taskDone(self, *args, **kargs):
        self.taskQueue.task_done()

    def increaseRunsNum(self):
        self.runningLock.acquire()
        self.running += 1  # 正在运行的线程数加1
        self.runningLock.release()

    def decreaseRunsNum(self):
        self.runningLock.acquire()
        self.running -= 1
        self.runningLock.release()

    def getTaskLeft(self):
        # 线程池的所有任务包括:
        # taskQueue中未被下载的任务, resultQueue中完成了但是还没被取出的任务, 正在运行的任务
        # 因此任务总数为三者之和
        return self.taskQueue.qsize() + self.running
开发者ID:hitalex,项目名称:tianya-forum-crawler,代码行数:87,代码来源:threadPool.py

示例5: Processer

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
class Processer(object):
    pause_object=object()
    def __init__(self, parent=None):
        # if current_processer:
        # raise Exception()
        self.children = []
        self.parent = parent
        if parent:
            parent.children.append(self)
        self.callStack = LifoQueue()
        self.callDepth = 0
        self.env = Globals.Globals
        self.ast = None
        self.stackPointer = 0
        self.cenv = None
        self.initialCallDepth = 0
    def getContinuation(self):
        if self.parent:
            pc = self.parent.continuation
        else:
            pc = dict(callDepth=0, callStack=[], initialCallDepth=0)
        return dict(env=self.cenv, callDepth=self.callDepth + pc['callDepth'],
                    callStack=deepcopy(self.callStack.queue) + pc['callStack'],
                    initialCallDepth=self.initialCallDepth + pc['initialCallDepth'], stackPointer=self.stackPointer)
    def setContinuation(self, args):
        continuation, retval = args
        self.callStack.queue[:] = deepcopy(continuation['callStack'])
        self.callDepth = continuation['callDepth']
        self.cenv = continuation['env']
        self.stackPointer = continuation['stackPointer']
        self.popStack(retval)
    continuation = property(getContinuation, setContinuation)
    def pushStackN(self):
        self.callStack.put((self.ast, self.cenv, self.stackPointer, 0))
        self.callDepth += 1
    def popStackN(self):
        self.ast, self.cenv, self.stackPointer, garbage = self.callStack.get_nowait()
        self.callDepth -= 1
    def pushStack(self, ast):
        if debug.DEBUG > 1:
            import traceback
            traceback.print_stack()
            print 'push', self.ast, self.stackPointer
        self.callStack.put((self.ast, self.cenv, self.stackPointer, 1))
        self.ast = ast
        self.cenv = Environment(self.cenv)
        self.stackPointer = 0
        self.callDepth += 1
    def popStack(self, retval, wrap=True):
        if debug.DEBUG > 1:
            import traceback
            traceback.print_stack()
            print 'pop', self.ast, retval, self.stackPointer,
            if len(self.callStack.queue):
                print self.callStack.queue[-1][0], self.callStack.queue[-1][2]
            print
            print
        if isinstance(retval, Symbol) and wrap:
            if isinstance(retval, SyntaxSymbol):
                retval = MacroSymbol(retval).setObj(retval)
            else:
                retval = MacroSymbol(retval).setEnv({retval: retval.toObject(self.cenv)})
        if debug.DEBUG>10:
            discarded_frames.append((self.ast, self.cenv, self.stackPointer))
        try:
            self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait()
        except Empty as e:
            e.ret = e.retval=self.ast[-1]
            raise e
        self.callDepth -= 1
        if rv:
            if self.stackPointer>=len(self.ast):
                self.popStack(retval)
            self.ast[self.stackPointer] = retval
        if debug.DEBUG > 1:
            print self.ast, self.stackPointer
    def dumpStack(self):
        while self.callDepth > 0 and self.callStack.queue:
            self.popStackN()
        self.stackPointer = 0
        self.cenv = None
        self.initialCallDepth = 0
        self.ast = None
        self.callDepth = 0
    tcd=0
    def doProcess(self, _ast, env=None, callDepth=None, ccc=False, quotesExpanded=False):
        print 102
        if not ccc:
            self.dumpStack()
        def LOG(*stuff):
            print  stuff
        LOG(97, _ast)
        try:
            p = self.iprocess(_ast, env, callDepth, quotesExpanded)
            v = p.next()
            while v is self.pause_object:
                yield v
                v=p.next()
            yield v
        except callCCBounce as e:
#.........这里部分代码省略.........
开发者ID:perkinslr,项目名称:pypyjs-release,代码行数:103,代码来源:processer.py

示例6: mainframe

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]

#.........这里部分代码省略.........

    def playpause(self, event=None):
        if self.player_process is None:
            return
        self.paused = not self.paused
        if self.paused:
            print "VIDEO IS PAUSED /B/RO"
            self.playbutton.configure(text="Play")
        else:
            self.playbutton.configure(text="Pause")
        self.command_player("pause")

    def setwh(self,w,h):
        self.videoFrame.configure(width=w, height=h)

    def quit(self):
        print "QUIT CALLED"
        self.destroy()

    def mplayer_isrunning(self):
        if self.player_process is not None:
            return (self.player_process.poll() is None)
        else:
            return False

    def command_player(self, comd):
        global fifofilename
        if self.mplayer_isrunning():
            try:
                self.player_process.stdin.flush()
                self.player_process.stdin.write("\r\n%s\r\n"%comd)
                # for _ in itertools.repeat(None,8192):
                #     self.player_process.stdin.write("\n")
                self.player_process.stdin.flush()
            except:
                showerror("Error","Error passing command to mplayer\n%s"%sys.exc_info()[1])

    def enqueue_pipe(self, out, q):
        print 'Working on reading mplayer pipe output...'
        for line in iter(out.readline, b''):
            q.put(line)
        out.close()

    def seekbar_setup(self):
        pos = '0'
        trial = 0
        while float(pos)<1:
            trial += 1
            pos = self.getvidtime()
        self.seekbar.config(to=int(float(pos)))
        self.endtime = int(float(pos))
        Timer(1, self.seekbar_updater).start()

    def seekbar_updater(self):
        if not self.paused and self.inhibit_slider_trigger:
            self.currtime += 1
            self.seekbar.set(self.currtime)
        else:
            self.currtime = self.seekbar.get()
        Timer(1, self.seekbar_updater).start()

    def seeked(self,e):
        pos = self.seekbar.get()
        print "We changed pos to :%d"% pos

        x, y = self.parent.winfo_pointerx(), self.parent.winfo_pointery()
        windowx, windowy = self.parent.winfo_width(), self.parent.winfo_height()
        if not self.inhibit_slider_trigger and windowy - 30 <= y:
            self.command_player("seek %d 2"%pos)
            if self.paused:
                self.command_player("pause")

    def startmousetrack(self):
        self.trackmouse = True

    def readpipe(self):
        line = ""
        try:
            line = self.q.get_nowait()
        except Empty:
            print "Empty PIPE"
        finally:
            return line

    def emptypipe(self):
        str = ''
        try:
            while not self.q.empty():
                str += self.q.get_nowait()
        except Empty:
            print "Empty Pipe"
        finally:
            return str

    def stop(self):
        if self.mplayer_isrunning():
            self.player_process.stdin.write("quit\n")
            self.player_process.stdin.flush()
            print self.emptypipe()
        self.player_process = None
开发者ID:ankit255,项目名称:mplay,代码行数:104,代码来源:mplay.py

示例7: Transceiver

# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import get_nowait [as 别名]
class Transceiver(object):

    def __init__(self, config={}, message_handler=None):
        """Set up a receiver which connects to the messaging hub.

        :param config: This is a dict in the form::

            config = dict(
                incoming='tcp://localhost:15566', # default
                outgoing='tcp://localhost:15567',
                idle_timeout=1000, # milliseconds:
            )

        """
        self.log = logging.getLogger("evasion.messenger.endpoint.Transceiver")

        self.endpoint_uuid = str(uuid.uuid4())

        self.exit_time = threading.Event()
        self.wait_for_exit = threading.Event()

        self.incoming = None # configured in main().
        self.incoming_uri = config.get("incoming", 'tcp://localhost:15566')
        self.log.info("Recieving on <%s>" % self.incoming_uri)

        self.outgoing_uri = config.get("outgoing", 'tcp://localhost:15567')
        self.log.info("Sending on <%s>" % self.outgoing_uri)

        self.idle_timeout = int(config.get("idle_timeout", 2000))
        self.log.info("Idle Timeout (ms): %d" % self.idle_timeout)

        self.message_handler = message_handler
        self.sync_message = frames.sync_message(
            "endpoint-%s" % self.endpoint_uuid
        )

        # Queue up messages to be sent in the main message loop
        self._out_queue = LifoQueue()


    def main(self):
        """Running the main loop sending and receiving.

        This will keep running until stop() is called. This
        sets the exit flag causing clean up and shutdown.

        """
        self.exitTime = False

        context = zmq.Context()
        incoming = context.socket(zmq.SUB)
        incoming.setsockopt(zmq.SUBSCRIBE, '')
        incoming.connect(self.incoming_uri)

        outgoing = context.socket(zmq.PUSH);
        outgoing.connect(self.outgoing_uri);

        def _shutdown():
            try:
                incoming.close()
            except ZMQError:
                self.log.exception("main: error calling incoming.close()")
            try:
                outgoing.close()
            except ZMQError:
                self.log.exception("main: error calling outgoing.close()")
            try:
                context.term()
            except ZMQError:
                self.log.exception("main: error calling context.term()")

        try:
            poller = zmq.Poller()
            poller.register(incoming, zmq.POLLIN)

            while not self.exit_time.is_set():
                try:
                    events = poller.poll(self.idle_timeout)

                except ZMQError as e:
                    # 4 = 'Interrupted system call'
                    if e.errno == 4:
                        self.log.info("main: exit time: %s" % e)
                        break
                    else:
                        self.log.info("main: <%s>" % e)
                        break

                except Exception:
                    self.log.exception("main: fatal error while polling ")
                    break

                else:
                    if (events > 0):
                        msg = incoming.recv_multipart()
                        self.message_in(tuple(msg))

                    # Now recover and queued outgoing messages:
                    if not self._out_queue.empty():
                        message = self._out_queue.get_nowait()
#.........这里部分代码省略.........
开发者ID:oisinmulvihill,项目名称:evasion-messenger,代码行数:103,代码来源:endpoint.py


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