Skip to content

Commit 0cd448a

Browse files
committed
Attempt to make PySR process a daemon
1 parent 967d63f commit 0cd448a

File tree

1 file changed

+67
-40
lines changed

1 file changed

+67
-40
lines changed

gui/processing.py

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import time
55
from pathlib import Path
66

7-
import numpy as np
87
import pandas as pd
98
from data import generate_data, read_csv
109

@@ -17,6 +16,37 @@
1716
)
1817

1918

19+
def pysr_fit(queue: mp.Queue, out_queue: mp.Queue):
20+
import pysr
21+
22+
while True:
23+
# Get the arguments from the queue, if available
24+
args = queue.get()
25+
if args is None:
26+
break
27+
X = args["X"]
28+
y = args["y"]
29+
kwargs = args["kwargs"]
30+
model = pysr.PySRRegressor(
31+
progress=False,
32+
timeout_in_seconds=1000,
33+
**kwargs,
34+
)
35+
model.fit(X, y)
36+
out_queue.put(None)
37+
38+
39+
class PySRProcess:
40+
def __init__(self):
41+
self.queue = mp.Queue()
42+
self.out_queue = mp.Queue()
43+
self.process = mp.Process(target=pysr_fit, args=(self.queue, self.out_queue))
44+
self.process.start()
45+
46+
47+
PERSISTENT_WRITER = None
48+
49+
2050
def processing(
2151
file_input,
2252
force_run,
@@ -41,6 +71,11 @@ def processing(
4171
batch_size,
4272
):
4373
"""Load data, then spawn a process to run the greet function."""
74+
global PERSISTENT_WRITER
75+
if PERSISTENT_WRITER is None:
76+
print("Starting PySR process")
77+
PERSISTENT_WRITER = PySRProcess()
78+
4479
if file_input is not None:
4580
try:
4681
X, y = read_csv(file_input, force_run)
@@ -53,31 +88,41 @@ def processing(
5388
base = Path(tmpdirname)
5489
equation_file = base / "hall_of_fame.csv"
5590
equation_file_bkup = base / "hall_of_fame.csv.bkup"
56-
process = mp.Process(
57-
target=pysr_fit,
58-
kwargs=dict(
91+
# Check if queue is empty, if not, kill the process
92+
# and start a new one
93+
if not PERSISTENT_WRITER.queue.empty():
94+
print("Restarting PySR process")
95+
if PERSISTENT_WRITER.process.is_alive():
96+
PERSISTENT_WRITER.process.terminate()
97+
PERSISTENT_WRITER.process.join()
98+
99+
PERSISTENT_WRITER = PySRProcess()
100+
# Write these to queue instead:
101+
PERSISTENT_WRITER.queue.put(
102+
dict(
59103
X=X,
60104
y=y,
61-
niterations=niterations,
62-
maxsize=maxsize,
63-
binary_operators=binary_operators,
64-
unary_operators=unary_operators,
65-
equation_file=equation_file,
66-
parsimony=parsimony,
67-
populations=populations,
68-
population_size=population_size,
69-
ncycles_per_iteration=ncycles_per_iteration,
70-
elementwise_loss=elementwise_loss,
71-
adaptive_parsimony_scaling=adaptive_parsimony_scaling,
72-
optimizer_algorithm=optimizer_algorithm,
73-
optimizer_iterations=optimizer_iterations,
74-
batching=batching,
75-
batch_size=batch_size,
76-
),
105+
kwargs=dict(
106+
niterations=niterations,
107+
maxsize=maxsize,
108+
binary_operators=binary_operators,
109+
unary_operators=unary_operators,
110+
equation_file=equation_file,
111+
parsimony=parsimony,
112+
populations=populations,
113+
population_size=population_size,
114+
ncycles_per_iteration=ncycles_per_iteration,
115+
elementwise_loss=elementwise_loss,
116+
adaptive_parsimony_scaling=adaptive_parsimony_scaling,
117+
optimizer_algorithm=optimizer_algorithm,
118+
optimizer_iterations=optimizer_iterations,
119+
batching=batching,
120+
batch_size=batch_size,
121+
),
122+
)
77123
)
78-
process.start()
79124
last_yield_time = None
80-
while process.is_alive():
125+
while PERSISTENT_WRITER.out_queue.empty():
81126
if equation_file_bkup.exists():
82127
try:
83128
# First, copy the file to a the copy file
@@ -109,21 +154,3 @@ def processing(
109154
last_yield_time = time.time()
110155
except pd.errors.EmptyDataError:
111156
pass
112-
113-
process.join()
114-
115-
116-
def pysr_fit(
117-
*,
118-
X,
119-
y,
120-
**pysr_kwargs,
121-
):
122-
import pysr
123-
124-
model = pysr.PySRRegressor(
125-
progress=False,
126-
timeout_in_seconds=1000,
127-
**pysr_kwargs,
128-
)
129-
model.fit(X, y)

0 commit comments

Comments
 (0)