Good day. Please help me with a problem.
I'm having trouble updating / reading data in a thread when using ORM.
I've been using peewee for Python. After reading the documentation starting to initialize the object to work with the database.
db = SqliteDatabase('db.sqlite', threadlocals=True)
# Next comes the creation of tables and filling.
class Products(Model):
id = IntegerField(index=True, primary_key=True)
count_sales = IntegerField()
updated = IntegerField() # default to zero
class Meta:
database = db
db.connect()
db.create_tables([Products, ...]) # I have several tables
# I continue to fill the table with data from a file
I then create a predetermined amount of threads as follows:
class MyThread(Thread):
tables = None
db = None
def run(self):
products_update(self.tables, self.db)
for _ in range(1, 20):
t = MyThread()
t.tables = (Products, ...)
t.db = db
t.start()
The function counts the number of records in the field count sales
, and there they are, it changes randomly one of them.
def products_update(tables, db):
Products, ... = tables
count = Products.select().where(Products.count_sales < 10, Products.updated == 0).limit(1).count()
if count:
row = Products.select().where(Products.count_sales < 10, Products.updated == 0).order_by(random()).limit(1).get()
Products.update(updated=1).where(Products.id == row.id).execute()
I need to make the process of finding and updating was linear.
Thank you for your attention. I apologize for the language I used a translator.