日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中的模块如何学习_在python中学习队列模块(如何运行它) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近引入了隊列設計,關于延遲處理能力以及實現“FIFO”等.

查看文檔以嘗試獲取示例隊列,以了解如何在我自己的設計/程序中實現它.但我遇到運行此代碼的問題:

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循環(huán)正在做什么,我只是運行代碼時出錯,所以我不得不遺漏一些東西.

出現問題錯誤:

NameError:未定義全局名稱’num_worker_threads’

謝謝-Python新手 –

for循環(huán)啟動了許多工作線程來執(zhí)行“worker”定義的功能.這是應該在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版本

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中的模块如何学习_在python中学习队列模块(如何运行它)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。