python常用库类

python常用库类

分类名称用途
科学计算Matplotlib用python实现的matlib三方库,用于绘制高质量的数学二维图形
科学计算SciPy基于python的matlab实现,旨在实现matlab的所有功能
科学计算NumPy基于python的科学计算第三方库,提供了矩阵、线性代数、傅里叶变换等
GUIPyGtk基于Python的GUI程序开发GTK+库
GUIPyQty用于Python的QT开发库
GUIWxPythonPython下的编程框架,与MFC架构相似
GUITkinterPython自带的界面编程包
其他BeautifulSoup基于Python的HTML/XML解析器,简单易用
其他PIL基于Python的图像处理库,功能强大,对图像格式支持广泛
其他MySQLdb用于链接Mysql 数据库
其他cElementTree高性能的xml解析器,python2.5之后包含该模块
其他PyCame基于Python的多媒体开发和游戏开发模块
其他Py2exe将Python转化为exe文件
其他pefilewindows PE 文件解析器

threading 模块

threading.Thread

threadong 中的Thread类,可以使用它来创建多线程。
具体使用方法是创建一个threading.Thread对象。

#conding = utf-8
import threading, time

count = 0
class MyThread(threading.Thread):
	def __init__(self,threadName):
		super(MyThread,self).__init__(name = threadName)

	def run(self):
		global count
		for i in range(100):
			count = count + 1
			time.sleep(0.3)
			print(self.getName(),count)

for i in range(2):
	MyThread("MyThreadName" +str(i)).start()
threading.Lock

使用lock.acquire() 和lock.release()包围的代码将进行同步操作,即此代码块执行完毕后,才能进行线程切换。

#coding = utf-8
import threading , time , random

count = 0
class MyThread(threading.Thread):
	def __init__(self,lock,threadName):
		super(MyThread,self).__init__(name = threadName)
		self.lock = lock

	def run(self):
		global count
		self.lock.acquire()
		for i in range(100):
			count +=1
			time.sleep(0.3)
			print(self.getName(),count)

		self.lock.release()



lock = threading.Lock()

for i in range(2):
	MyThread(lock,"MythreadName:" + str(i)).start()

threading.join

join类是threading中用于阻塞当前主线程的类,其作用是阻塞当前所用线程,直到被调用的线程执行完毕或者超时。

import threading,time

def doWaiting():
	print('start waiting:', time.strftime('%S'))
	time.sleep(3)
	print('stop waiting', time.strftime('%S'))
	
thread1 = threading.Thread(target=doWaiting)
thread1.start()
time.sleep(1)
print('start join')
thread1.join()
print('join end')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值