閒來無事研究了下py,發現多執行緒處理起資料來比php不要爽太多,廢話少說上碼
__author__ = 'yao'
import mydb
from time import ctime,sleep
def mythread(db):
for i in xrange(10):
sql = "select * from y_user where c=0 limit 10"
db.query(sql)
result = db.fetchallrows()
for row in result:
db.update("update y_user set c=1 where id=%s" % row[0])
if __name__ == '__main__':
dbconfig =
db = mydb.mysql(dbconfig)
mythread(db)
開10個執行緒同時處理,在資料量大的時候處理效率比單純在web端走php快很多,當然也可以玩php-cli。。。純屬本人閒著沒事搗鼓,大牛就別看了
附上找來的db封裝類
import mysqldb
import time
class mysql:
error_code = none
_instance = none
_conn = none
_cur = none
_timeout = 30
_timecount = 0
def __init__(self, dbconfig):
try:
self._conn = mysqldb.connect(host=dbconfig['host'],
port=dbconfig['port'],
user=dbconfig['user'],
passwd=dbconfig['passwd'],
db=dbconfig['db'],
charset=dbconfig['charset'])
except mysqldb.error, e:
self.error_code = e.args[0]
error_msg = 'mysql error! ', e.args[0], e.args[1]
print error_msg
if self._timecount < self._timeout:
interval = 5
self._timecount += interval
time.sleep(interval)
return self.__init__(dbconfig)
else:
raise exception(error_msg)
self._cur = self._conn.cursor()
self._instance = mysqldb
def query(self,sql):
try:
self._cur.execute("set names utf8")
result = self._cur.execute(sql)
except mysqldb.error, e:
self.error_code = e.args[0]
result = false
return result
def update(self,sql):
try:
self._cur.execute("set names utf8")
result = self._cur.execute(sql)
self._conn.commit()
except mysqldb.error, e:
self.error_code = e.args[0]
result = false
return result
def insert(self,sql):
try:
self._cur.execute("set names utf8")
self._cur.execute(sql)
self._conn.commit()
return self._conn.insert_id()
except mysqldb.error, e:
self.error_code = e.args[0]
return false
def fetchallrows(self):
return self._cur.fetchall()
def fetchonerow(self):
return self._cur.fetchone()
def getrowcount(self):
return self._cur.rowcount
def commit(self):
self._conn.commit()
def rollback(self):
self._conn.rollback()
def __del__(self):
try:
self._cur.close()
self._conn.close()
except:
pass
def close(self):
self.__del__()
python多執行緒處理資料
python多執行緒處理資料 從檔案讀取資料,多執行緒處理 usr bin env python encoding utf 8 import threading import time from queue import queue def readfile file object open opt...
C 多執行緒處理資料
os centos 7 編譯環境 gcc 4.8 cpu 2顆 intel r xeon r cpu e5 2670 v3 2.30ghz,24核48執行緒。int pthread create pthread t thread,const pthread attr t restrict attr,...
多執行緒Callable處理資料
1.定義乙個等於cpu核心數的執行緒池 2.根據資料 list 處理每個執行緒可以分到的數量list 3.callable 執行緒處理資料 4.future 獲取callcable執行緒處理後的資料 5.把 future 獲取的資料重新 addall 進 list 6.返回資料 public lis...