Ahojte,
Mam takyto jednoduchy skript s ktorym ovladam vystupy. Mam tam funkciu output_on v ktorej spustam styri vlakna na postupne zapinanie styroch vystupov. Ak to spravim raz, tak to funguje super, ale ak to chcem spravit este raz, tak mi napise, ze vlakna mozu byt spustene len raz. Som myslel, ze ked to vlakna vykonaju, tak sa ukoncia a mozem ich spustit znova.
from ablib import Pin import time #import sys import sqlite3 import threading #data = sys.argv[1] temp_list = list() conn = sqlite3.connect('database/database.db') cur = conn.cursor() cur.execute("SELECT restart_timer FROM output") row = cur.fetchall() def output_on(data): if data == "out1_on": output1=Pin('N4','OUTPUT') output1.on() if data == "out2_on": output2=Pin('N5','OUTPUT') output2.on() if data == "out3_on": output3=Pin('N6','OUTPUT') output3.on() if data == "out4_on": output4=Pin('N7','OUTPUT') output4.on() if data == "glb_on": output1_thread.start() output2_thread.start() output3_thread.start() output4_thread.start() def output1(): output1=Pin('N4','OUTPUT') time.sleep(list(row[0])[0]) output1.on() def output2(): output2=Pin('N5','OUTPUT') time.sleep(list(row[1])[0]) output2.on() def output3(): output3=Pin('N6','OUTPUT') time.sleep(list(row[2])[0]) output3.on() def output4(): output4=Pin('N7','OUTPUT') time.sleep(list(row[2])[0]) output4.on() def output_off(data): if data == "out1_off": output1=Pin('N4','OUTPUT') output1.off() if data == "out2_off": output2=Pin('N5','OUTPUT') output2.off() if data == "out3_off": output3=Pin('N6','OUTPUT') output3.off() if data == "out4_off": output4=Pin('N7','OUTPUT') output4.off() if data == "glb_off": output1=Pin('N4','OUTPUT') output2=Pin('N5','OUTPUT') output3=Pin('N6','OUTPUT') output4=Pin('N7','OUTPUT') output1.off() output2.off() output3.off() output4.off() output1_thread = threading.Thread(target = output1) output2_thread = threading.Thread(target = output2) output3_thread = threading.Thread(target = output3) output4_thread = threading.Thread(target = output4)
Thread sa dá spustiť len raz. V tomto konkrétnom prípade by som radšej použil multiprocessing.Pool.map.
output1_thread = threading.Thread(target = output1).start() output2_thread = threading.Thread(target = output2).start() output3_thread = threading.Thread(target = output3).start() output4_thread = threading.Thread(target = output4).start()
pripadne pouzi len toto ↓↓↓↓ bez nazvu
threading.Thread(target = output1).start() threading.Thread(target = output2).start() threading.Thread(target = output3).start() threading.Thread(target = output4).start()
neviem, ci potrebujes mat nazov threadu...pises ze nakolko by mali byt vypnute...ony sa ukoncia same...ano...ale thread moze byt spusteny len raz...no, nic ti nebrani vytvorit novy, co robi to iste a spustit ho
sorry za mišmaš
output1_thread = threading.Thread(target = output1).start()
output2_thread = threading.Thread(target = output2).start()
output3_thread = threading.Thread(target = output3).start()
output4_thread = threading.Thread(target = output4).start()
pripadne pouzi len toto ↓↓↓↓ bez nazvu
threading.Thread(target = output1).start()
threading.Thread(target = output2).start()
threading.Thread(target = output3).start()
threading.Thread(target = output4).start()
neviem, ci potrebujes mat nazov threadu...pises ze nakolko by mali byt vypnute...ony sa ukoncia same...ano...ale thread moze byt spusteny len raz...no, nic ti nebrani vytvorit novy, co robi to iste a spustit ho
no a hod to sem↓↓↓samozrejme, ak to dobre chapem if data == "glb_on":
tak nakoniec som to spravil takto. Funguje o celkom dobre.