Repository navigation
Expand file tree
/
Copy pathlive.py
More file actions
63 lines (52 loc) · 1.83 KB
/
Copy pathlive.py
File metadata and controls
63 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# live.py
import sys, os, time, subprocess, threading, sysconfig
class Live:
def __init__(self, script):
self.script = script
self.process = None
self.running = True
self.last_mtime = os.path.getmtime(script)
self.python_exe = sys.executable
print(f"🐍 Usando Python: {self.python_exe}")
print(f"▶️ Ejecutando: {script}")
self.start()
t = threading.Thread(target=self.watch)
t.daemon = True
t.start()
try:
while self.running:
time.sleep(0.1)
if self.process and self.process.poll() is not None:
print("⚠️ App cerrada, reiniciando...")
self.start()
except KeyboardInterrupt:
self.stop()
def start(self):
if self.process:
self.process.terminate()
time.sleep(0.2)
if self.process.poll() is None:
self.process.kill()
self.process = subprocess.Popen([self.python_exe, self.script])
def watch(self):
while self.running:
time.sleep(0.3)
try:
mtime = os.path.getmtime(self.script)
if mtime != self.last_mtime:
self.last_mtime = mtime
print("🔄 Cambio detectado, recargando...")
self.start()
except:
pass
def stop(self):
self.running = False
if self.process:
self.process.terminate()
print("👋 Detenido")
sys.exit(0)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Uso: python live.py script.py")
sys.exit(1)
Live(sys.argv[1])