在上一章中,学习了Python多进程编程的一些基本方法:使用跨平台多进程模块multiprocessing提供的Process、Pool、Queue、Lock、Pipe等类,实现子进程创建、进程池(批量创建子进程并管理子进程数量上限)以及进程间通信。这一章学习下Python下的多线程编程方法。
一、threading
线程是操作系统执行任务的最小单元。Python标准库中提供了threading模块,对多线程编程提供了很便捷的支持。
下面是使用threading实现多线程的代码:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 8 import threading, time 9 10 def test(index):11 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))12 print 'thread %s starts.' % threading.current_thread().name13 print 'the index is %d' % index14 time.sleep(3)15 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))16 print 'thread %s ends.' % threading.current_thread().name17 18 if name == "main":19 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))20 print 'thread %s starts.' % threading.current_thread().name21 #创建线程22 my_thread = threading.Thread(target = test, args=(1,) , name= 'zni_feng_thread')23 #等待2s24 time.sleep(2)25 #启动线程26 my_thread.start()27 #等待线程结束28 my_thread.join()29 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))30 print 'thread %s ends.' % threading.current_thread().name
输出结果为:
2017-01-12 22:06:32 thread MainThread starts. 2017-01-12 22:06:34 thread zni_feng_thread starts. the index is 1 2017-01-12 22:06:37 thread zni_feng_thread ends. 2017-01-12 22:06:37 thread MainThread ends. [Finished in 5.1s]
其中,threading模块的current_thread()函数会返回当前线程的实例。
二、Lock
多进程与多线程的最大不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响。而多线程中,所有变量都由所有线程共享,所以,任何一个共享变量都可以被任何一个线程修改。因此线程之间共享数据最大的危险在于多个线程同时改变一个变量。为了解决这个问题,我们可以借助于threading模块的Lock类给共享变量加锁。
先看看使用多线程写同一个共享变量,不加锁的例子:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 import threading 8 9 class Account:10 def init(self):11 self.balance = 012 13 def add(self):14 for i in range(0,100000):15 self.balance += 116 17 def delete(self):18 for i in range(0,100000):19 self.balance -=1 20 21 if name == "main":22 account = Account()23 #创建线程24 thread_add = threading.Thread(target=account.add, name= 'Add')25 thread_delete = threading.Thread(target=account.delete, name= 'Delete')26 27 #启动线程28 thread_add.start()29 thread_delete.start()30 31 #等待线程结束32 thread_add.join()33 thread_delete.join()34 35 print 'The final balance is: ' + str(account.balance)
运行结果为:
The final balance is: -51713 [Finished in 0.1s]
可以发现,每次运行,它的最终结果都会不同,而且都不是0。就是因为不同线程在同时修改同一个变量时,发生了冲突,某些中间变量没有按顺序被使用导致。
现在我们使用Lock对程序进行加锁:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 import threading 8 9 class Account:10 def init(self):11 self.balance = 012 13 def add(self, lock):14 #获得锁15 lock.acquire()16 for i in range(0,100000):17 self.balance += 118 #释放锁19 lock.release()20 21 def delete(self, lock):22 #获得锁23 lock.acquire()24 for i in range(0,100000):25 self.balance -=1 26 #释放锁27 lock.release()28 29 30 if name == "main":31 account = Account()32 lock = threading.Lock()33 #创建线程34 thread_add = threading.Thread(target=account.add, args=(lock, ), name= 'Add')35 thread_delete = threading.Thread(target=account.delete, args=(lock, ), name= 'Delete')36 37 #启动线程38 thread_add.start()39 thread_delete.start()40 41 #等待线程结束42 thread_add.join()43 thread_delete.join()44 45 print 'The final balance is: ' + str(account.balance)
可以发现,无论如何执行多少次,balance结果都为0。如果将每次balance计算的结果都打印出来,还会发现,当一个线程开始执行时,另一个线程一定会等到前一个线程执行完(准确地说是lock.release()执行完)后才开始执行。
The final balance is: 0 [Finished in 0.1s]
【相关推荐】
2. Python中推荐使用多进程而不是多线程?分享推荐使用多进程的原因
以上是Python中多进程与多线程实例(二)编程方法的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。