本文实例讲述了python开发之thread线程基础。分享给大家供大家参考,具体如下:
说到线程,我们要知道啥是串行,啥是并行程序
举个例子:
串行程序,就是一个一个的执行程序
#python threading import time ''' 每一秒中,输出:this is a demo! ''' def serial(): '''串行输出''' time.sleep(1) print('this is a demo!') def main(): for i in range(5): serial() if __name__ == '__main__': main()
运行结果如下:
>>> this is a demo! this is a demo! this is a demo! this is a demo! this is a demo! >>>
并行程序,就是很多个程序在同一时间(宏观)一起执行
#python threading import threading import time ''' 并行执行,输出:Good!Good!Good!Good!Good! ''' def parallel(): '''并行输出''' time.sleep(1) print('Good!') def main(): for i in range(5): t = threading.Thread(target=parallel) t.start() if __name__ == '__main__': main()
当然我们通过执行程序,可以知道,并行程序要比串行程序执行的要快....
我们也可以获取到当前的线程及个数:
#python threading import threading import time ''' 并行执行,输出: [<Thread(Thread-2, started 3480)>, <Thread(Thread-1, started 660)>, <Thread(SockThread, started daemon 2920)>, <Thread(Thread-3, started 916)>, <Thread(Thread-4, started 3476)>, <_MainThread(MainThread, started 3964)>, <Thread(Thread-5, started 2060)>] 存在的线程数 : 7 Good!Good!Good!Good!Good! ''' def parallel(): '''并行输出''' time.sleep(1) print('Good!') def main(): for i in range(5): t = threading.Thread(target=parallel) t.start() if __name__ == '__main__': main() print(threading.enumerate()) print('存在的线程数 : %d'%threading.active_count())
运行结果如下:
>>> [<Thread(SockThread, started daemon 15424)>, <Thread(Thread-3, started 15840)>, <Thread(Thread-1, started 10884)>, <Thread(Thread-2, started 14512)>, <Thread(Thread-4, started 13204)>, <_MainThread(MainThread, started 12924)>, <Thread(Thread-5, started 15476)>] 存在的线程数 : 7 >>> Good!Good!Good!Good!Good!
希望本文所述对大家Python程序设计有所帮助。

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

The impact of homogeneity of arrays on performance is dual: 1) Homogeneity allows the compiler to optimize memory access and improve performance; 2) but limits type diversity, which may lead to inefficiency. In short, choosing the right data structure is crucial.

TocraftexecutablePythonscripts,followthesebestpractices:1)Addashebangline(#!/usr/bin/envpython3)tomakethescriptexecutable.2)Setpermissionswithchmod xyour_script.py.3)Organizewithacleardocstringanduseifname=="__main__":formainfunctionality.4

NumPyarraysarebetterfornumericaloperationsandmulti-dimensionaldata,whilethearraymoduleissuitableforbasic,memory-efficientarrays.1)NumPyexcelsinperformanceandfunctionalityforlargedatasetsandcomplexoperations.2)Thearraymoduleismorememory-efficientandfa

NumPyarraysarebetterforheavynumericalcomputing,whilethearraymoduleismoresuitableformemory-constrainedprojectswithsimpledatatypes.1)NumPyarraysofferversatilityandperformanceforlargedatasetsandcomplexoperations.2)Thearraymoduleislightweightandmemory-ef

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)
