Home >Backend Development >Python Tutorial >What is the python thread priority queue? What are the methods for setting thread priority?

What is the python thread priority queue? What are the methods for setting thread priority?

乌拉乌拉~
乌拉乌拉~Original
2018-08-21 19:44:263769browse

For those who are exposed to the python programming language for the first time, they know little about python threads when they first started learning python programming. In this article, we will learn about pythonThread priority and pythonThread priority setting knowledge.

Thread Priority Queue (Queue)

Python’s Queue module provides synchronous, thread-safe queue classes, including FIFO (first in, first out) queue Queue , LIFO (last in first out) queue LifoQueue, and priority queue PriorityQueue. These queues implement lock primitives and can be used directly in multi-threads. Queues can be used to achieve synchronization between threads.

Commonly used methods in the Queue module:

1.Queue.qsize() returns the size of the queue

2. Queue.empty() If the queue is empty, return True, otherwise False

3.Queue.full() If the queue is full, return True, otherwise False

4.Queue.full and maxsize size corresponds to

5.Queue.get([block[, timeout]]) to get the queue, timeout waiting time

6.Queue.get_nowait() is equivalent to Queue.get(False)

7.Queue.put(item) writes to the queue, timeout waiting time

8.Queue.put_nowait(item) is equivalent to Queue.put(item, False)

9 .Queue.task_done() After completing a job, the Queue.task_done() function sends a signal to the queue where the task has been completed

10.Queue.join() actually means waiting until the queue is empty, Then perform other operations

The example is as follows:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import Queue
import threading
import time

exitFlag = 0

class myThread(threading.Thread):
   def __init__(self, threadID, name, q):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.q = q
   def run(self):
      print "Starting " + self.name
      process_data(self.name, self.q)
      print "Exiting " + self.name
def process_data(threadName, q):
   while not exitFlag:
      queueLock.acquire()
      if not workQueue.empty():
         data = q.get()
         queueLock.release()
         print "%s processing %s" % (threadName, data)
      else:
         queueLock.release()
      time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1
# 创建新线程
for tName in threadList:
   thread = myThread(threadID, tName, workQueue)
   thread.start()
   threads.append(thread)
   threadID += 1
# 填充队列
queueLock.acquire()
for word in nameList:
   workQueue.put(word)
queueLock.release()
# 等待队列清空
while not workQueue.empty():
   pass
# 通知线程是时候退出
exitFlag = 1
# 等待所有线程完成
for t in threads:
   t.join()
print "Exiting Main Thread"

The output result of the above example is as follows:

Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
Exiting Thread-3
Exiting Thread-1
Exiting Thread-2
Exiting Main Thread

The above is what this article describes All content, this article mainly introduces the relevant knowledge of python thread priority. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is the python thread priority queue? What are the methods for setting thread priority?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more