python之多线程thread

没有评论

2012 年 08 月 28 日 at 下午 10:47分类:Python

# -*- coding:utf-8 -*-
#!/usr/bin/python
# Filename: thread.py

#导入threading模块
import threading

T = threading.Thread

#继承threading模块中的Thread类
class MyThread(T):
	name = None
	def __init__(self, name = None):
		T.__init__(self)
		self.name = name

	def run(self):
		print self.name

def start():
	for i in range(1,10):
		#生存Thread对象,并且启动该线程
		MyThread('thread_'+str(i)).start()


if __name__ == '__main__':
	start()

print '#'*30

import thread, time

def timer(i = None):
	print 'hello-'+str(i)
	# thread.exit()

def test():
	for i in range(1,10):
		thread.start_new_thread(timer, (i,))
		#另一种使用threading创建线程的方式:
		#threading.Thread(timer,(i,)).start()

if __name__ == '__main__':
	test()
	time.sleep(2)

print '*'*30

#如果不加锁导致最终的count结果不是10000*10,而代码以下由于是使用了锁,
#从而保证了同一个时间只能有一个线程修改count的值,所以最终结果是10000*10.
count = 0
lock = thread.allocate_lock()
def qt():
	global count, lock

	lock.acquire()

	for x in range(0,1000):
		count += 1

	lock.release()

for i in range(0,10):
	thread.start_new_thread(qt, ())

time.sleep(3)
print count

因此在python创建一个线程的方法有3种:
1、使用thread模块的start_new_thread()方法来创建,如:thread.start_new_thread(timer, (i,))
2、使用threading模块的Thread类来创建,如:threading.Thread(timer,(i,)).start()
3、自定义类继承threading类来实现创建,如上面一个例子

windows上安装easy_install ,python 2.7

没有评论

2012 年 08 月 28 日 at 下午 9:16分类:Python

最近想在windows 配置一些python的程序,无奈很多东西都没有在linux下的方便, 于是开始了配置这样的环境。

安装 setuptool http://pypi.python.org/packages/2.7/s/setuptools/;

第一步会在你的python 目录下scripts 有一个 easy_install,用cmd 进入该目录

D:\Python27\Scripts>easy_install networkx 就可以安装你想安装的库了

Python学习之备份脚本

没有评论

2012 年 08 月 08 日 at 下午 10:26分类:Python

功能是:可以为指定的目录、文件创建备份的程序

'''
Created on 2012-8-8
#!/usr/bin/python
@author: fbbin
@desc bakup
'''

# 1. import some modules
import os,sys,time

# 2. The files and directories to be backed up are specified in a list.
source = ['/home/www','/home/bin','/home/info']
targetDir = '/home/python/'

# The current day is the name of the subdirectory in the main directory
todayPath = targetDir + time.strftime('%Y%m%d')
# The current time is the name of the tarball archive
now = time.strftime('%H%M%S')

if len(sys.argv) >= 2:
	if sys.argv[1] == '-v':
		print 'version:1.0.1'
	elif sys.argv[1] == '-d':
		# check this day path is exists
		if not os.path.exists(todayPath):
			os.mkdir(todayPath)
			print 'Successfully created directory', todayPath

		# Take a comment from the user to create the name of the tarball file
		comment = raw_input('Enter a comment --> ')

		if len(comment) == 0:
			target = todayPath + os.sep + now + '.tar.gz'
		else:
			target = todayPath + os.sep + now + '_' + comment.replace(' ', '_') + '.tar.gz'

		commond = "tar -cvzf %s %s " % (target, ' '.join(source))

		#exec the linux commond
		if os.system(commond) == 0:
			print 'Successful backup to %s' % target
		else:
			print 'Backup Failed'
	else:
		print 'Nothing Done'
else:
	print '''
		This program prints files to the standard output.
		Any number of files can be specified.
		Options include:
		--v : Prints the version number
		--d : go the bakup'''
	sys.exit()