python相似模块用例
一:threading VS Thread
众所周知,python是支持多线程的,而且是native的线程,其中threading是对Thread模块做了包装,可以更加方面的被使用,threading模块里面主要对一些线程操作对象化了,创建了Thread的类。
使用线程有两种模式,一种是创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行,一种是直接从Thread继承,创建一个新的class,把线程执行的代码放到这个新的类里面,用例如下:
①使用Thread来实现多线程
#!/usr/bin/env python #-*- coding:utf-8 -*- import string import threading import time def threadMain(a): global count,mutex #获得线程名 threadname = threading.currentThread().getName() for x in xrange(0,int(a)): #获得锁 mutex.acquire() count += 1 #释放锁 mutex.release() print threadname,x,count time.sleep() def main(num): global count,mutex threads = [] count = 1 #创建一个锁 mutex = threading.Lock() #先创建线程对象 for x in xrange(0,num): threads.append(threading.Thread(target = threadMain,args=(10,))) for t in threads: t.start() for t in threads: t.join() if __name__ == "__main__": num = 4 main(num);
②使用threading来实现多线程
#!/usr/bin/env python #-*- coding:utf-8 -*- import threading import time class Test(threading.Thread): def __init__(self,num): threading.Thread.__init__(self): self._run_num = num def run(self): global count,mutex threadName = threading.currentThread.getName() for x in xrange(0,int(self._run_num)): mutex.acquire() count += 1 mutex.release() print threadName,x,count time.sleep(1) if __name__ == "__main__": global count,mutex threads = [] num = 4 count = 1 mutex.threading.Lock() for x in xrange(o,num): threads.append(Test(10)) #启动线程 for t in threads: t.start() #等待子线程结束 for t in threads: t.join()
二:optparser VS getopt
①使用getopt模块处理Unix模式的命令行选项
getopt模块用于抽出命令行选项和参数,也就是sys.argv,命令行选项使得程序的参数更加灵活,支持短选项模式和长选项模式
例:python scriptname.py –f “hello” –directory-prefix=”/home” –t --format ‘a'‘b'
getopt函数的格式:getopt.getopt([命令行参数列表],‘短选项',[长选项列表])
其中短选项名后面的带冒号(:)表示该选项必须有附加的参数
长选项名后面有等号(=)表示该选项必须有附加的参数
返回options以及args
options是一个参数选项及其value的元组((‘-f','hello'),(‘-t',''),(‘—format',''),(‘—directory-prefix','/home'))
args是除去有用参数外其他的命令行 输入(‘a',‘b')
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys import getopt def Usage(): print "Usage: %s [-a|-0|-c] [--help|--output] args..."%sys.argv[0] if __name__ == "__main__": try: options,args = getopt.getopt(sys.argv[1:],"ao:c",['help',"putput="]): print options print "\n" print args for option,arg in options: if option in ("-h","--help"): Usage() sys.exit(1) elif option in ('-t','--test'): print "for test option" else: print option,arg except getopt.GetoptError: print "Getopt Error" Usage() sys.exit(1)
②optparser模块
#!/usr/bin/env python # -*- coding:utf-8 -*- import optparser def main(): usage = "Usage: %prog [option] arg1,arg2..." parser = OptionParser(usage=usage) parser.add_option("-v","--verbose",action="store_true",dest="verbose",default=True,help="make lots of noise [default]") parser.add_option("-q","--quiet",action="store_false",dest="verbose",help="be vewwy quiet (I'm hunting wabbits)") parser.add_option("-f","--filename",metavar="FILE",help="write output to FILE") parser.add_option("-m","--mode",default="intermediate",help="interaction mode: novice, intermediate,or expert [default: %default]") (options,args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") if options.verbose: print "reading %s..." %options.filename if __name__ == "__main__": main()
以上就是threading VS Thread、optparser VS getopt 的相互比较,希望对大家学习模块有所帮助。

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

在 Sublime Text 中运行 Python 代码,需先安装 Python 插件,再创建 .py 文件并编写代码,最后按 Ctrl B 运行代码,输出会在控制台中显示。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

Golang在性能和可扩展性方面优于Python。1)Golang的编译型特性和高效并发模型使其在高并发场景下表现出色。2)Python作为解释型语言,执行速度较慢,但通过工具如Cython可优化性能。

在 Visual Studio Code(VSCode)中编写代码简单易行,只需安装 VSCode、创建项目、选择语言、创建文件、编写代码、保存并运行即可。VSCode 的优点包括跨平台、免费开源、强大功能、扩展丰富,以及轻量快速。

在 Notepad 中运行 Python 代码需要安装 Python 可执行文件和 NppExec 插件。安装 Python 并为其添加 PATH 后,在 NppExec 插件中配置命令为“python”、参数为“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通过快捷键“F6”运行 Python 代码。
