Answer the question
In order to leave comments, you need to log in
How to properly work with threads in python?
Greetings, I used streams a little, but I don’t have a final understanding of how to cope with them.
python3 + django 1.9
When the server starts, 1 main thread starts and 2 child threads from it, what it looks like
def scheduled():
while True:
# выполняется некоторая фильтрация по бд в итоге получаем список send, который передаем дочернему потоку
t2 = threading.Thread(target=sending, args=[send])
t2.start()
t3 = threading.Thread(target=statistics)
t3.start()
t = threading.Thread(target=scheduled)
t.start()
Answer the question
In order to leave comments, you need to log in
Having studied in more detail the materials on flows, having listened to the advice, the problem was solved quite simply.
The script will be launched via manage.py, so you don’t have to rewrite the current functionality and with this launch a separate process will be created in the system, let’s run it in the background using surepvisord/systemd.
import threading
import time
def example(name ,event):
i = 0
while event.is_set():
print('Thread: %s, %d', % (t1.name, i))
i += 1
time.sleep(10)
try:
event = threading.Event()
event.set()
t1 = threading.Thread(target=example, args=('first', event))
t1.start()
except KeyboardInterrupt:
event.clear()
t1.join()
I don't know what your task is, but it's better to use Celery or other similar tools.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question