我如何在指定的时间段内调用该函数?
how do i call the function in specified period of time?
我正在尝试构建一个将调用函数并将权重存储到数据库中的学习器,现在的问题是,它至少需要 30 到 60 秒才能学习,所以如果我想存储我需要等待,我决定用线程定时器调用函数,它将在指定的时间段后调用函数,
代码示例:
def learn(myConnection):
'''
Derive all the names,images where state = 1
Learn and Store
Delete all the column where state is 1
'''
id = 0
with myConnection:
cur = myConnection.cursor()
cur.execute("Select name, image FROM images WHERE state = 1")
rows = cur.fetchall()
for row in rows:
print "%s, %s" % (row[0], row[1])
name ='images/Output%d.jpg' % (id,)
names = row[0]
with open(name, "wb") as output_file:
output_file.write(row[1])
unknown_image = face_recognition.load_image_file(name)
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
# here i give a timer and call the function
threading=Timer(60, storeIntoSQL(names,unknown_encoding) )
threading.start()
id += 1
没有用这个的事情是它就像我没有指定计时器一样工作它没有等待 60 秒它只是正常工作就像我在没有计时器的情况下调用函数一样,关于如何的任何想法我可以使这项工作或我可以使用哪些替代方案? ... PS 我已经使用了 time.sleep 它只是停止了主线程 我需要项目是 运行 而这是训练
正在调用的函数示例:
def storeIntoSQL(name,unknown_face_encoding):
print 'i am printing'
# connect to the database
con = lite.connect('users2.db')
# store new person into the database rmena
with con:
cur = con.cursor()
# get the new id
cur.execute("SELECT DISTINCT id FROM Users ")
rows = cur.fetchall()
newId = len(rows)+1
# store into the Database
query = "INSERT INTO Users VALUES (?,?,?)"
cur.executemany(query, [(newId,name,r,) for r in unknown_face_encoding])
con
我还被告知 MUTEX 同步可以提供帮助,只有当另一个线程完成它的工作时我才能让一个线程工作,但我不确定如何实现它并且愿意接受任何建议
我建议使用 python 的 threading
库并在函数内部或包装函数中的某处实现 time.sleep(60)
。例如
import time
import threading
def delayed_func(name,unknown_face_encoding):
time.sleep(60)
storeIntoSQL(name,unknown_face_encoding)
timer_thread = threading.Thread(target=delayed_func, args=(name,unknown_face_encoding))
timer_thread.start()
我正在尝试构建一个将调用函数并将权重存储到数据库中的学习器,现在的问题是,它至少需要 30 到 60 秒才能学习,所以如果我想存储我需要等待,我决定用线程定时器调用函数,它将在指定的时间段后调用函数,
代码示例:
def learn(myConnection):
'''
Derive all the names,images where state = 1
Learn and Store
Delete all the column where state is 1
'''
id = 0
with myConnection:
cur = myConnection.cursor()
cur.execute("Select name, image FROM images WHERE state = 1")
rows = cur.fetchall()
for row in rows:
print "%s, %s" % (row[0], row[1])
name ='images/Output%d.jpg' % (id,)
names = row[0]
with open(name, "wb") as output_file:
output_file.write(row[1])
unknown_image = face_recognition.load_image_file(name)
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
# here i give a timer and call the function
threading=Timer(60, storeIntoSQL(names,unknown_encoding) )
threading.start()
id += 1
没有用这个的事情是它就像我没有指定计时器一样工作它没有等待 60 秒它只是正常工作就像我在没有计时器的情况下调用函数一样,关于如何的任何想法我可以使这项工作或我可以使用哪些替代方案? ... PS 我已经使用了 time.sleep 它只是停止了主线程 我需要项目是 运行 而这是训练
正在调用的函数示例:
def storeIntoSQL(name,unknown_face_encoding):
print 'i am printing'
# connect to the database
con = lite.connect('users2.db')
# store new person into the database rmena
with con:
cur = con.cursor()
# get the new id
cur.execute("SELECT DISTINCT id FROM Users ")
rows = cur.fetchall()
newId = len(rows)+1
# store into the Database
query = "INSERT INTO Users VALUES (?,?,?)"
cur.executemany(query, [(newId,name,r,) for r in unknown_face_encoding])
con
我还被告知 MUTEX 同步可以提供帮助,只有当另一个线程完成它的工作时我才能让一个线程工作,但我不确定如何实现它并且愿意接受任何建议
我建议使用 python 的 threading
库并在函数内部或包装函数中的某处实现 time.sleep(60)
。例如
import time
import threading
def delayed_func(name,unknown_face_encoding):
time.sleep(60)
storeIntoSQL(name,unknown_face_encoding)
timer_thread = threading.Thread(target=delayed_func, args=(name,unknown_face_encoding))
timer_thread.start()