Answer the question
In order to leave comments, you need to log in
How to queue a task to an existing parallel thread in Python 3?
All the best! I'm just learning Python, and now I ran into a problem:
I have a function for working with google tables, in it I read the number of filled rows and enter new data in row_count +1 . But the process of writing to the table takes a long time, and I decided to perform this action in a new thread in order to be able to accept new requests to write to the table. But I get a new thread every time, and I just need to queue up in this separate thread, because the request to get the number of filled rows is fast, it turns out that I write different data to the same row , since row_counteverything is looking for the previous one, because the previous record is only found in the process and has not ended yet
def append_to_sheet(id):
action_time = datetime.now().strftime("%Y.%m.%d %H:%M:%S")
row = [action_time, id]
row_count = len(sheet.get_all_values())
index = row_count +1
sheet.insert_row(row, index)
def start_with_thread():
while True:
id = q.get()
append_to_sheet(id)
q.task_done()
t = threading.Thread(target=start_with_thread)
t.setDaemon(True)
t.start()
q.put(id)
Answer the question
In order to leave comments, you need to log in
It is necessary to:
1) create the thread only once, at the start of the program
2) use a Queue to communicate with it: https://docs.python.org/2/library/queue.html
An alternative to be much simpler is to use Celery www.celeryproject.org _
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question