multiprocess kullanarak iki tane fonk ayni anda calisdiriyorum fakat soyle sorunum var fonnk bir tanesi zaman islemi yapiyor ayarladigim zamanda programin sonlanmasini istiyorum time modulunu kullanarak bir islem yapdim ve ben isteyim zamana gelince exit() kullanarak programi sonlandirmak istiyorum fakat None diye cikdi veriyor ffonk tek basina kullanirken ise calisiyor acaba multiprocess-de kullandigim fonklari nasil sonlandira bilirim?
Multiprocessing bilmiyorum ama threading de bir thread i sonlandirmak icin buna benzer bir algoritma uygulamıstım.Multiprocessing e sen uyarlarsın.
import threading as t
import time
stop = False
def bitir():
global stop
stop = True
my_timer = t.Timer(10,bitir)
my_timer.start()
def func1():
global stop
stop = False
while 1:
print("calısıyorum")
time.sleep(1)
if stop:
print("calismayı kestim")
break
f1 = t.Thread(target=func1)
f1.daemon = True
f1.run()
Classla yaptigim diger bir ornek-ki bence classlı halini kullanın.bence global degiskenini kullanmaktan kacının.Classlar daha iyi,daha pythonic.-
import threading as t
import time
class deneme():
def __init__(self):
my_timer = t.Timer(10,self.bitir)
my_timer.start()
self.stop = False
f1 = t.Thread(target=self.func1)
f1.daemon = True
f1.run()
def func1(self):
while 1:
print("f1 calısıyor")
time.sleep(1)
if self.stop:
print("f1 calismayı kesti")
break
def bitir(self):
self.stop = True
deneme()