在python中学习队列模块(如何运行它)

在python中学习队列模块(如何运行它),第1张

概述最近引入了队列设计,关于延迟处理能力以及实现“FIFO”等. 查看文档以尝试获取示例队列,以了解如何在我自己的设计/程序中实现它.但我遇到运行此代码的问题: import queuedef worker(): while True: item = q.get() do_work(item) q.task_done()def main( 最近引入了队列设计,关于延迟处理能力以及实现“FIFO”等.

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

import queuedef 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 donemain()

问题:希望有人解释for循环正在做什么,我只是运行代码时出错,所以我不得不遗漏一些东西.

出现问题错误:
nameError:未定义全局名称’num_worker_threads’

谢谢-Python新手 –

解决方法 for循环启动了许多工作线程来执行“worker”定义的功能.这是应该在python 2.7中运行在您的系统上的工作代码.

import Queueimport threading# input queue to be processed by many threadsq_in = Queue.Queue(maxsize=0)# output queue to be processed by one threadq_out = Queue.Queue(maxsize=0)# number of worker threads to complete the processingnum_worker_threads = 10# process that each worker thread will execute until the Queue is emptydef 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 squaredef do_work(item):    return (item,item*item)# another queued thread we will use to print outputdef 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 processesdef 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版本

import queue import threading# input queue to be processed by many threadsq_in = queue.Queue(maxsize=0) # output queue to be processed by one threadq_out = queue.Queue(maxsize=0) # number of worker threads to complete the processingnum_worker_threads = 10# process that each worker thread will execute until the Queue is emptydef worker():    while True:        # get item from queue,item*item)# another queued thread we will use to print outputdef 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 processesdef 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中学习队列模块(如何运行它)全部内容,希望文章能够帮你解决在python中学习队列模块(如何运行它)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1195899.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存