python 模拟线程并发
关键字: python最近做python,下面是一段测试数据库性能的代码。
数据库表记录:1000000条。
开200个线程,100个同时并发。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import os.path
import re
import MySQLdb
import httplib, mimetypes
import traceback
import logging
import ConfigParser
import time
import threading
import string
import codecs
import random
import datetime
class SelectView(threading.Thread):
def __init__(self, threadName):
threading.Thread.__init__(self, name = threadName)
def run(self):
#global dbConn
#dbConn = MySQLdb.connect(host=dbhost, port=dbport, user=dbuser, passwd=dbpass, db=dbschema, use_unicode=True, charset='utf8')
for i in range(100):
start =time.time()
print self.getName(),"-->Thread start!"+getDbDateString()
logger.info(self.getName()+"-->Thread start!"+getDbDateString())
try:
dbConn = None
dbConn = MySQLdb.connect(host=dbhost, port=dbport, user=dbuser, passwd=dbpass, db=dbschema, use_unicode=True, charset='utf8')
strss = string.join(random.sample(['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','T','Z'], 1)).replace(" ","")
#print "query str----"+strss
sql = "SELECT * FROM smartone_metainfo_view as s where songName like '%"+strss+"%' limit 200"
cursor = dbConn.cursor()
cursor.execute(sql)
songid =""
for counts in cursor.fetchall():
songid= songid + str(counts[0])+str(counts[1])+str(counts[2])+str(counts[3])+str(counts[4])+str(counts[5])+str(counts[6])+str(counts[7])+str(counts[8])+str(counts[9])+str(counts[10])+str(counts[11])+str(counts[12])+str(counts[13])+str(counts[14])+str(counts[15])+ str(counts[16])+str(counts[17])+str(counts[18])+str(counts[19])+str(counts[20])+str(counts[21])+str(counts[22])+str(counts[23])+str(counts[24])+str(counts[25])+str(counts[26])+str(counts[27])+str(counts[28])+str(counts[29])+str(counts[30])+str(counts[31])
end =time.time()
sec = end - start
print " thread,execute total time is----",sec," second"
logger.info(" thread execute finish,execute total time is----"+str(sec)+" second")
print songid
#print "size --"+str(len(counts))+"songId---"+str(counts[0])# self.getName(),"--query result:--total count--"+str(counts[0])#+"----songName:"+counts[1]+"----isrc"+str(counts[2])
#logger.info("size --"+str(len(counts))+"songId---"+str(counts[0]))
#print "command free----",os.system('free')
dbConn.close()
except:
etype, evalue = sys.exc_info()[:2]
logger.info(self.getName()+"--Thread,-query data failed!---Type: " +str(etype)+"----Value--:"+str(evalue))
print "Type: " , etype
print "Value: " , evalue
#cmd = "mysqladmin -uMELODEO_USER -p6DF0FF9674EC1A397A430F9F2FCB2C64 -h202.153.121.172 status"
# strs = os.system(cmd)
#lines = os.popen(cmd).readlines()
#for line in lines:
# print line.rstrip()
#logger.info("".join(lines))
#sem.release()
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def init_settings(profile, config_file):
#read ini file
# config_file = "c:/contentstext/processCommonFormat.ini"
# print os.getcwd()
cfg = ConfigParser.RawConfigParser()
cfg.read(config_file)
#define the directory we are writing out to and some other parameters.
global dbhost
dbhost = cfg.get(profile, "dbhost")
global dbport
dbport = int( cfg.get(profile, "dbport"))
global dbuser
dbuser = cfg.get(profile, "dbuser")
global dbpass
dbpass = cfg.get(profile, "dbpassword")
global dbschema
dbschema = cfg.get(profile, "db")
global USE_UNICODE
try:
use_unicode = cfg.get(profile, "USE_UNICODE")
if (use_unicode == 'Y'):
USE_UNICODE=True
else:
USE_UNICODE=False
except:
USE_UNICODE=False
#set up logs - ripped from contentManager.py
LOG_FILE = './smartone_200.log'
val = cfg.get(profile, "loglevel") # The configured log level
if len(val) >0:
if val == 'debug':
log_level = logging.DEBUG
elif val == 'info':
log_level = logging.INFO
elif val == 'warning':
log_level = logging.WARNING
elif val == 'error':
log_level = logging.ERROR
elif val == 'critical':
log_level = logging.CRITICAL
else:
log_level = logging.INFO
val = cfg.get(profile, "log_format") # The format string for this logging session
if len(val) >0:
log_format = val
else:
log_format = '%(asctime)s %(levelname)s %(message)s'
###################
# Initialize logger
global logger
logger = logging.getLogger('sessioninfo')
hdlr = logging.FileHandler(LOG_FILE)
formatter = logging.Formatter(log_format)
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(log_level)
def getDbDateString():
now = datetime.datetime.today()
#should probably do this using a formatting string - don't feel like reading docs.
return str(now.year) + "-" + str(now.month) + "-" + str(now.day) + " " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
if __name__ == "__main__":
config_file = "./smartone_DataTest.ini"
profile = "smartone"
init_settings(profile, config_file)
maxThread = 200
#threading.
import socket
socket.setdefaulttimeout(15)
threadList=[]
#threading.BoundedSemaphore(maxThread)
for i in range(maxThread):
#sv = SelectView()
threads = SelectView('thread'+str(i))
threadList.append(threads)
#threadList.remove(len(threadList))
print '\nStarting threads'
for i in threadList:
i.start()
print 'All threads started\n'
for i in threadList:
i.join()
print "All thread has create,Wait for all thread exit."
#
#sv.executeThread()
# sv = SelectView()
# cout = sv.selectView()
# print "wewe----",cout
发表评论
- 浏览: 19033 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
共 1 张
最新评论
-
还是相信中国女足!
小虫1313 写道瑞典队的"女伊布"球感真不错... 身体素质也是同样的出色
-- by satellite -
还是相信中国女足!
珍爱生命,远离国足!!
-- by 脑袋雷劈的 -
还是相信中国女足!
ham 写道88958620 写道希望中国男足明天也该中国人民带来惊喜! 这.. ...
-- by leon_a -
还是相信中国女足!
引用 网易新疆吐鲁番网友 ip:124.118.*.*: 2008-08-06 ...
-- by 一叶孤鸿 -
还是相信中国女足!
差不多, 我最后一次正经的看也是2002年 哈哈, 还记得吗, 范志毅, 郝海东 ...
-- by lhyasia






评论排行榜