微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python脚本在循环访问SQLite数据库时被locking

请通过我的parsing器的代码观看。 它从循环访问他们的网页抓取一些统计数据,并在sqlite3数据库中放入指定的logging。

一切正常,直到第87行(sql语句),进程消耗所有的cpu资源,实际上被阻止。

文件“./parser.py”,第86行,在while(j <i):

代码开头的数据库文件是用正确的结构创build的,所以问题出现在循环中。 for season in season_list:的主循环内部块for season in season_list:工作得很好。 这是我的脚本的整个代码

如何从linux内核生成稳定的37kHz GPIO触发器?

在Linux中查找由可执行程序设置的套接字选项

FIFO文件和读/写

文件复制到一个子目录中并重新命名

传输文件使用scp

#!/usr/bin/env python from bs4 import BeautifulStonesoup from urllib2 import urlopen import re import sqlite3 from time import gmtime,strftime # Print start time print "We started at ",strftime("%Y-%m-%d %H:%M:%s",gmtime()) # Create DB print "Trying to create DB" con = sqlite3.connect('england.db') cur = con.cursor() sql = """ CREATE TABLE english_premier_league ( id_match INTEGER PRIMARY KEY AUTOINCREMENT,season TEXT,tour INTEGER,date TEXT,home TEXT,visitor TEXT,home_score INTEGER,visitor_score INTEGER ); """ try: cur.executescript(sql) except sqlite3.DatabaseError as err: print "Error creating database: ",err else: print "Succesfully created your database..." con.commit() cur.close() con.close() # list of variables postfix = 2011 threshold = 1999 season_list = [] while postfix >= threshold: end = (postfix + 1) % 2000 if (end >= 10): season = str(postfix) + str(end) else: season = str(postfix) + str(0) + str(end) season_list.append(season) postfix -= 1 print season_list # main loop for season in season_list: href = 'http://www.stat-football.com/en/a/eng.PHP?b=10&d='+season+'&c=51' print href xml = urlopen(href).read() xmlSoup = BeautifulStonesoup(xml) tablet = xmlSoup.find(attrs={"class" : "bd5"}) #Access DB con = sqlite3.connect('england.db') cur = con.cursor() #Parse site tour = tablet.findAll(attrs = { "class" : re.compile(r"^(s3|cc s3)$") }) date = tablet.findAll(text = re.compile(r"(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).(19|20)dd")) home = tablet.findAll(attrs = {"class" : "nw"}) guest = tablet.findAll(attrs = {"class" : "s1"}) score = tablet.findAll(attrs = {"class" : "nw pr15"}) # def parse_string(sequence): result=[] for unit in sequence: text = ''.join(unit.findAll(text=True)) result.append(text.strip()) return result tour_list=parse_string(tour) home_list=parse_string(home) guest_list=parse_string(guest) score_list=parse_string(score) #Loop over found records to put them into sqlite3 DB i = len(tour_list) j = 0 while (j < i): sql_add = 'INSERT INTO english_premier_league (season,tour,date,home,visitor,home_score,visitor_score) VALUES (?,?,?)' match = (season,int(tour_list[j]),date[j],home_list[j],guest_list[j],int(score_list[j][0:1]),int(score_list[j][2:3])) try: cur.executemany(sql_add,match) except sqlite3.DatabaseError as err: print "Error matching the record: ",err else: con.commit() part = float(j)/float(i)*100 if (part%10 == 0): print (int(part)),"%" j += 1 cur.close() con.close()

也可以看看strace输出的结尾:

getcwd(“/ home / vitaly / football_forecast / epl”,512)= 35 stat(“/ home / vitaly / football_forecast / epl / england.db”,{st_mode = S_IFREG | 0644,st_size = 24576,…}) (3,F_SETFD,FD_CLOEXEC)= 0 fstat(3,{0} = 0 open(“/ home / st_mode = S_IFREG | 0644,st_size = 24576,…})= 0 lseek(3,0,SEEK_SET)= 0 read(3,“sqlite format 3 0 4 0 1 1 0 @ 0 0 1〜 0 0 0 30“…,100)= 100

我在Ubuntu 12.04上运行Python 2.7。 非常感谢。

使用ioctl()可以使用9引脚串行端口作为“GPIO”?

exec“`dirname”$ 0“`”/../../ waf“$ @”

Linux内核在进程死亡后进行处理和TCP连接清理的位置在哪里?

使用QProcess在C ++中redirectgnome-terminal的输出

当从Python运行Perl脚本并将输出写入文件时,为什么文件是空的?

将cur.executemany(sql_add,match)替换为cur.execute(sql_add,match) 。 executemany()用于在可迭代的值上多次执行相同的操作。 例如,如果你有这个:

match = [ (season1,tour1,date1,home1,visitor1,home_score1,visitor_score1),(season2,tour2,date2,home2,visitor2,home_score2,visitor_score2),(season3,tour3,date3,home3,visitor3,home_score3,visitor_score3) ] cur.executemany(sql_add,match)

…这是适当的,因为游标可以遍历匹配中的元组,并对它们中的每一个执行插入操作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐