龙空技术网

Python多线程编程(二十八)

IT技术院 151

前言:

此时你们对“python 子线程与主线程共享数据”大致比较关怀,兄弟们都想要知道一些“python 子线程与主线程共享数据”的相关资讯。那么小编也在网上网罗了一些有关“python 子线程与主线程共享数据””的相关资讯,希望你们能喜欢,各位老铁们一起来学习一下吧!

同时运行多个线程类似于同时运行多个不同的程序,但具有以下好处 -

进程内的多个线程与主线程共享相同的数据空间,因此可以比单独的进程更容易地共享信息或彼此进行通信。线程有时也被称为轻量级进程,它们不需要太多的内存开销; 它们比进程便宜。

线程有一个开始,执行顺序和终止。 它有一个指令指针,可以跟踪其上下文中当前运行的位置。

它可以被抢占(中断)。当其他线程正在运行时,它可以临时保留(也称为睡眠) - 这称为让步。

有两种不同的线程 -

内核线程用户线程

内核线程是操作系统的一部分,而用户空间线程未在内核中实现。

有两个模块用于支持在Python 3中使用线程 -

_threadthreading

thread模块已被“不推荐”了很长一段时间。 鼓励用户使用threading模块。 因此,在Python 3中,thread模块不再可用。 但是,thread模块已被重命名为“_thread”,用于Python 3中的向后兼容性。

1.启动新线程

要产生/启动一个线程,需要调用thread模块中的以下方法 -

_thread.start_new_thread ( function, args[, kwargs] )

这种方法调用可以快速有效地在Linux和Windows中创建新的线程。

方法调用立即返回,子线程启动并使用传递的args列表调用函数。当函数返回时,线程终止。

在这里,args是一个元组的参数; 使用空的元组来调用函数表示不传递任何参数。 kwargs是关键字参数的可选字典。

示例

#!/usr/bin/python3import _threadimport time# Define a function for the threaddef print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print ("%s: %s" % ( threadName, time.ctime(time.time()) ))# Create two threads as followstry: _thread.start_new_thread( print_time, ("Thread-1", 2, ) ) _thread.start_new_thread( print_time, ("Thread-2", 4, ) )except: print ("Error: unable to start thread")while 1: pass

当执行上述代码时,会产生以下结果 -

F:\worksp\python>python thread_start.pyThread-1: Tue Jun 27 03:06:09 2018Thread-2: Tue Jun 27 03:06:11 2018Thread-1: Tue Jun 27 03:06:11 2018Thread-1: Tue Jun 27 03:06:13 2018Thread-2: Tue Jun 27 03:06:15 2018Thread-1: Tue Jun 27 03:06:15 2018

程序进入无限循环,可通过按ctrl-c停止或退出。虽然它对于低级线程非常有效,但与较新的线程模块相比,thread模块非常有限。

2. threading模块

Python 2.4中包含的较新的线程模块为线程提供了比上面讨论的线程模块更强大的高级支持。

线程模块公开了线程模块的所有方法,并提供了一些其他方法 -

threading.activeCount() - 返回活动的线程对象的数量。threading.currentThread() - 返回调用者线程控件中线程对象的数量。threading.enumerate() - 返回当前处于活动状态的所有线程对象的列表。

除了这些方法之外,threading模块还有实现线程的Thread类。 Thread类提供的方法如下:

run() - run()方法是线程的入口点。start() - start()方法通过调用run()方法启动一个线程。join([time]) - join()等待线程终止。isAlive() - isAlive()方法检查线程是否仍在执行。getName() - getName()方法返回一个线程的名称。setName() - setName()方法设置线程的名称。

3.使用threading模块创建线程

要使用threading模块实现新线程,必须执行以下操作:

定义Thread类的新子类。覆盖__init __(self [,args])方法添加其他参数。然后,重写run(self [,args])方法来实现线程在启动时应该执行的操作。

当创建了新的Thread的子类之后,就可以创建一个实例,然后调用start()方法来调用run()方法来启动一个新的线程。

示例

#!/usr/bin/python3import threadingimport timeexitFlag = 0class MyThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print ("Starting " + self.name) print_time(self.name, self.counter, 5) print ("Exiting " + self.name)def print_time(threadName, delay, counter): while counter: if exitFlag: threadName.exit() time.sleep(delay) print ("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1# Create new threadsthread1 = MyThread(1, "Thread-1", 1)thread2 = MyThread(2, "Thread-2", 2)# Start new Threadsthread1.start()thread2.start()thread1.join()thread2.join()print ("Exiting Main Thread")

当运行上述程序时,它会产生以下结果 -

Starting Thread-1Starting Thread-2Thread-1: Tue Jun 27 03:19:43 2017Thread-2: Tue Jun 27 03:19:44 2017Thread-1: Tue Jun 27 03:19:44 2017Thread-1: Tue Jun 27 03:19:45 2017Thread-2: Tue Jun 27 03:19:46 2017Thread-1: Tue Jun 27 03:19:46 2017Thread-1: Tue Jun 27 03:19:47 2017Exiting Thread-1Thread-2: Tue Jun 27 03:19:48 2017Thread-2: Tue Jun 27 03:19:50 2017Thread-2: Tue Jun 27 03:19:52 2017Exiting Thread-2Exiting Main Thread

4.同步线程

Python提供的threading模块包括一个简单易用的锁定机制,允许同步线程。 通过调用lock()方法创建一个新的锁,该方法返回新的锁。

新锁对象的acquire(blocking)方法用于强制线程同步运行。可选的blocking参数能够控制线程是否要等待获取锁定。

如果blocking设置为0,则如果无法获取锁定,则线程将立即返回0值,如果锁定已获取,则线程返回1。 如果blocking设置为1,则线程将blocking并等待锁定被释放。

新的锁定对象的release()方法用于在不再需要锁定时释放锁。

示例

#!/usr/bin/python3# save file : MyThread2.pyimport threadingimport timeclass MyThread2 (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print ("Starting " + self.name) # Get lock to synchronize threads threadLock.acquire() print_time(self.name, self.counter, 3) # Free lock to release next thread threadLock.release()def print_time(threadName, delay, counter): while counter: time.sleep(delay) print ("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1threadLock = threading.Lock()threads = []# Create new threadsthread1 = MyThread2(1, "Thread-1", 1)thread2 = MyThread2(2, "Thread-2", 2)# Start new Threadsthread1.start()thread2.start()# Add threads to thread listthreads.append(thread1)threads.append(thread2)# Wait for all threads to completefor t in threads: t.join()print ("Exiting Main Thread")

当执行上述代码时,会产生以下结果 -

Starting Thread-1Starting Thread-2Thread-1: Tue Jun 27 03:51:45 2017Thread-1: Tue Jun 27 03:51:46 2017Thread-1: Tue Jun 27 03:51:47 2017Thread-2: Tue Jun 27 03:51:49 2017Thread-2: Tue Jun 27 03:51:51 2017Thread-2: Tue Jun 27 03:51:53 2017Exiting Main Thread

5.多线程优先级队列

queue模块允许创建一个新的队列对象,可以容纳特定数量的项目。 有以下方法来控制队列 -

get() - get()从队列中删除并返回一个项目。put() - put()将项添加到队列中。qsize() - qsize()返回当前队列中的项目数。empty() - 如果队列为空,则empty()方法返回True; 否则返回False。full() - 如果队列已满,则full()方法返回True; 否则返回False。

示例

#!/usr/bin/python3#coding=utf-8import queueimport threadingimport timeexitFlag = 0class MyQueue (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# Create new threadsfor tName in threadList: thread = MyQueue(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1# Fill the queuequeueLock.acquire()for word in nameList: workQueue.put(word)queueLock.release()# Wait for queue to emptywhile not workQueue.empty(): pass# Notify threads it's time to exitexitFlag = 1# Wait for all threads to completefor t in threads: t.join()print ("Exiting Main Thread")

当执行上述代码时,会产生以下结果 -

Starting Thread-1Starting Thread-2Starting Thread-3Thread-3 processing OneThread-3 processing TwoThread-3 processing ThreeThread-3 processing FourThread-3 processing FiveExiting Thread-1Exiting Thread-2Exiting Thread-3Exiting Main Thread

标签: #python 子线程与主线程共享数据