i have script queries on database "blogs" , each blog, starts thread query rss address , check new posts , record them on database. initially, running script maximum of 2 parallel threads (retrieving information rss of @ least 2 blogs @ same time) , then, started getting "database locked error", reduced one, , still error.
for database connection , orm using peewee 2.7.4 this:
from peewee import * playhouse.sqlite_ext import sqliteextdatabase db = sqliteextdatabase(app_dir + '/ml.db') class basemodel(model): class meta: database = db class blog(basemodel): (...) class post(basemodel): (...)
so, starting script does:
def start(): global active_threads, max_threads blogs = blog.select().where(block.active=1) blog in blogs: while active_threads == max_threads: print 'max number of threads %d reached. zzzz' % max_threads time.sleep(1) blog.processing=1 blog.save() active_threads += 1 th = threading.thread(target=process_blog, args=(blog,)) th.daemon = true th.start() def process_blog(blog): globals active_threads get_new_posts_url_for_blog(blog) # here post records created downloaded=0 posts = post.select().where(post.downloaded = 0) post in posts: content = get_content_for_post(post.url) post.content = content post.downloaded = 1 post.save() #this database locked error thrown :( active_threads -= 1
this simplified version of script, of course, , @ first loop on "posts", following error on post.save():
file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 4573, in save rows = self.update(**field_dict).where(self._pk_expr()).execute() file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3013, in execute return self.database.rows_affected(self._execute()) file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 2555, in _execute return self.database.execute_sql(sql, params, self.require_commit) file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3366, in execute_sql self.commit() file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3212, in __exit__ reraise(new_type, new_type(*exc_args), traceback) file "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3359, in execute_sql cursor.execute(sql, params or ()) operationalerror: database locked
keep in mind now, running max_threads=1 there 1 blog being processed @ time. bothers me on first runs, run max_threads=2 , go way through, fine. error started few days later, don't know if maybe on blogs select, on main thread, things locked (maybe select attached , have detach'em somehow). please me this? small process , not change database engine, , see performance benefits, crucial, running in @ least 2 threads in parallel.
your appreciated.
thank you, ts
try wrapping writes atomic()
context manager.
Comments
Post a Comment