• <tfoot id='nkw6o'></tfoot>

      • <bdo id='nkw6o'></bdo><ul id='nkw6o'></ul>
    1. <legend id='nkw6o'><style id='nkw6o'><dir id='nkw6o'><q id='nkw6o'></q></dir></style></legend>

      <small id='nkw6o'></small><noframes id='nkw6o'>

      <i id='nkw6o'><tr id='nkw6o'><dt id='nkw6o'><q id='nkw6o'><span id='nkw6o'><b id='nkw6o'><form id='nkw6o'><ins id='nkw6o'></ins><ul id='nkw6o'></ul><sub id='nkw6o'></sub></form><legend id='nkw6o'></legend><bdo id='nkw6o'><pre id='nkw6o'><center id='nkw6o'></center></pre></bdo></b><th id='nkw6o'></th></span></q></dt></tr></i><div id='nkw6o'><tfoot id='nkw6o'></tfoot><dl id='nkw6o'><fieldset id='nkw6o'></fieldset></dl></div>

      1. 学习python中的Queue模块(如何运行)

        时间:2023-09-29

          <tbody id='1lQgD'></tbody>
        • <bdo id='1lQgD'></bdo><ul id='1lQgD'></ul>
          <tfoot id='1lQgD'></tfoot><legend id='1lQgD'><style id='1lQgD'><dir id='1lQgD'><q id='1lQgD'></q></dir></style></legend>
            • <i id='1lQgD'><tr id='1lQgD'><dt id='1lQgD'><q id='1lQgD'><span id='1lQgD'><b id='1lQgD'><form id='1lQgD'><ins id='1lQgD'></ins><ul id='1lQgD'></ul><sub id='1lQgD'></sub></form><legend id='1lQgD'></legend><bdo id='1lQgD'><pre id='1lQgD'><center id='1lQgD'></center></pre></bdo></b><th id='1lQgD'></th></span></q></dt></tr></i><div id='1lQgD'><tfoot id='1lQgD'></tfoot><dl id='1lQgD'><fieldset id='1lQgD'></fieldset></dl></div>

                <small id='1lQgD'></small><noframes id='1lQgD'>

                  本文介绍了学习python中的Queue模块(如何运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  最近在队列设计中引入了延迟处理的能力以及实现FIFO"等.

                  Was recently introduced to the queue design in regards to ability to defer processing as well as implementing a "FIFO" etc.

                  查看文档以尝试获取示例队列以了解如何在我自己的设计/程序中实现它.但是我在运行这段代码时遇到了问题:

                  Looked through the documentation in attempt to get a sample queue going to understand how to implement it in my own design / program. But I'm having issues with just running this code:

                  import queue
                  
                  def worker():
                      while True:
                          item = q.get()
                          do_work(item)
                          q.task_done()
                  
                  def main():
                  
                      q = queue.Queue(maxsize=0)
                      for i in range(num_worker_threads):
                           t = Thread(target=worker)
                           t.daemon = True
                           t.start()
                  
                      for item in source():
                          q.put(item)
                  
                      q.join()       # block until all tasks are done
                  
                  main()
                  

                  问题:希望有人解释 for 循环在做什么,我在运行代码时遇到错误,所以我必须遗漏一些东西.

                  Question: Would like someone to explain what the for loops are doing, I get an error just running the code so I have to be missing something.

                  发生的问题错误:NameError:未定义全局名称num_worker_threads"

                  感谢-Python新手-

                  Thank you from a -Python Novice-

                  推荐答案

                  for循环正在启动一些工作线程来执行worker"定义的功能.这是应该在您的系统上以 python 2.7 运行的工作代码.

                  The for loop is launching a number of worker threads to perform the function defined by "worker". Here is working code that should run on your system in python 2.7.

                  import Queue
                  import threading
                  
                  # input queue to be processed by many threads
                  q_in = Queue.Queue(maxsize=0)
                  
                  # output queue to be processed by one thread
                  q_out = Queue.Queue(maxsize=0)
                  
                  # number of worker threads to complete the processing
                  num_worker_threads = 10
                  
                  # process that each worker thread will execute until the Queue is empty
                  def worker():
                      while True:
                          # get item from queue, do work on it, let queue know processing is done for one item
                          item = q_in.get()
                          q_out.put(do_work(item))
                          q_in.task_done()
                  
                  # squares a number and returns the number and its square
                  def do_work(item):
                      return (item,item*item)
                  
                  # another queued thread we will use to print output
                  def printer():
                      while True:
                          # get an item processed by worker threads and print the result. Let queue know item has been processed
                          item = q_out.get()
                          print "%d squared is : %d" % item
                          q_out.task_done()
                  
                  # launch all of our queued processes
                  def main():
                      # Launches a number of worker threads to perform operations using the queue of inputs
                      for i in range(num_worker_threads):
                           t = threading.Thread(target=worker)
                           t.daemon = True
                           t.start()
                  
                      # launches a single "printer" thread to output the result (makes things neater)
                      t = threading.Thread(target=printer)
                      t.daemon = True
                      t.start()
                  
                      # put items on the input queue (numbers to be squared)
                      for item in range(10):
                          q_in.put(item)
                  
                      # wait for two queues to be emptied (and workers to close)   
                      q_in.join()       # block until all tasks are done
                      q_out.join()
                  
                      print "Processing Complete"
                  
                  main()
                  

                  每个@handle 的 Python 3 版本

                  Python 3 version per @handle

                  import queue 
                  import threading
                  
                  # input queue to be processed by many threads
                  q_in = queue.Queue(maxsize=0) 
                  
                  # output queue to be processed by one thread
                  q_out = queue.Queue(maxsize=0) 
                  
                  # number of worker threads to complete the processing
                  num_worker_threads = 10
                  
                  # process that each worker thread will execute until the Queue is empty
                  def worker():
                      while True:
                          # get item from queue, do work on it, let queue know processing is done for one item
                          item = q_in.get()
                          q_out.put(do_work(item))
                          q_in.task_done()
                  
                  # squares a number and returns the number and its square
                  def do_work(item):
                      return (item,item*item)
                  
                  # another queued thread we will use to print output
                  def printer():
                      while True:
                          # get an item processed by worker threads and print the result. Let queue know item has been processed
                          item = q_out.get()
                          print("{0[0]} squared is : {0[1]}".format(item) )
                          q_out.task_done()
                  
                  # launch all of our queued processes
                  def main():
                      # Launches a number of worker threads to perform operations using the queue of inputs
                      for i in range(num_worker_threads):
                           t = threading.Thread(target=worker)
                           t.daemon = True
                           t.start()
                  
                      # launches a single "printer" thread to output the result (makes things neater)
                      t = threading.Thread(target=printer)
                      t.daemon = True
                      t.start()
                  
                      # put items on the input queue (numbers to be squared)
                      for item in range(10):
                          q_in.put(item)
                  
                      # wait for two queues to be emptied (and workers to close)   
                      q_in.join()       # block until all tasks are done
                      q_out.join()
                  
                      print( "Processing Complete" )
                  
                  main()
                  

                  这篇关于学习python中的Queue模块(如何运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Celery 创建动态队列 下一篇:Python 中内置的最大堆 API

                  相关文章

                      <bdo id='Ufgxk'></bdo><ul id='Ufgxk'></ul>
                  1. <tfoot id='Ufgxk'></tfoot>

                    <i id='Ufgxk'><tr id='Ufgxk'><dt id='Ufgxk'><q id='Ufgxk'><span id='Ufgxk'><b id='Ufgxk'><form id='Ufgxk'><ins id='Ufgxk'></ins><ul id='Ufgxk'></ul><sub id='Ufgxk'></sub></form><legend id='Ufgxk'></legend><bdo id='Ufgxk'><pre id='Ufgxk'><center id='Ufgxk'></center></pre></bdo></b><th id='Ufgxk'></th></span></q></dt></tr></i><div id='Ufgxk'><tfoot id='Ufgxk'></tfoot><dl id='Ufgxk'><fieldset id='Ufgxk'></fieldset></dl></div>
                    1. <legend id='Ufgxk'><style id='Ufgxk'><dir id='Ufgxk'><q id='Ufgxk'></q></dir></style></legend>

                    2. <small id='Ufgxk'></small><noframes id='Ufgxk'>