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脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

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

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

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

Dreamweaver CS6
视觉化网页开发工具

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

热门话题

本文将阐述如何通过分析Debian系统下的Apache日志来提升网站性能。一、日志分析基础Apache日志记录了所有HTTP请求的详细信息,包括IP地址、时间戳、请求URL、HTTP方法和响应代码等。在Debian系统中,这些日志通常位于/var/log/apache2/access.log和/var/log/apache2/error.log目录下。理解日志结构是有效分析的第一步。二、日志分析工具您可以使用多种工具分析Apache日志:命令行工具:grep、awk、sed等命令行工具可

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

本文探讨DDoS攻击检测方法,虽然未找到“DebianSniffer”的直接应用案例,但以下方法可用于DDoS攻击检测:有效的DDoS攻击检测技术:基于流量分析的检测:通过监控网络流量的异常模式,例如突发性的流量增长、特定端口的连接数激增等,来识别DDoS攻击。这可以使用多种工具实现,包括但不限于专业的网络监控系统和自定义脚本。例如,Python脚本结合pyshark和colorama库可以实时监控网络流量并发出警报。基于统计分析的检测:通过分析网络流量的统计特征,例如数据

Debian系统中的readdir函数是用于读取目录内容的系统调用,常用于C语言编程。本文将介绍如何将readdir与其他工具集成,以增强其功能。方法一:C语言程序与管道结合首先,编写一个C程序调用readdir函数并输出结果:#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

本文将指导您如何在Debian系统上更新NginxSSL证书。第一步:安装Certbot首先,请确保您的系统已安装certbot和python3-certbot-nginx包。若未安装,请执行以下命令:sudoapt-getupdatesudoapt-getinstallcertbotpython3-certbot-nginx第二步:获取并配置证书使用certbot命令获取Let'sEncrypt证书并配置Nginx:sudocertbot--nginx按照提示选

在Debian系统上配置HTTPS服务器涉及几个步骤,包括安装必要的软件、生成SSL证书、配置Web服务器(如Apache或Nginx)以使用SSL证书。以下是一个基本的指南,假设你使用的是ApacheWeb服务器。1.安装必要的软件首先,确保你的系统是最新的,并安装Apache和OpenSSL:sudoaptupdatesudoaptupgradesudoaptinsta
