Spaces:
Sleeping

gbtami commited on
Commit
a52652f
·
1 Parent(s): 3471d4d

Fix #106 Exit/Close safely

Browse files
Files changed (1) hide show
  1. lichess-bot.py +10 -2
lichess-bot.py CHANGED
@@ -10,6 +10,7 @@ import logging
10
  import multiprocessing
11
  import traceback
12
  import logging_pool
 
13
  import time
14
  import backoff
15
  from config import load_config
@@ -42,6 +43,13 @@ def watch_control_stream(control_queue, li):
42
  else:
43
  control_queue.put_nowait({"type": "ping"})
44
 
 
 
 
 
 
 
 
45
  def start(li, user_profile, engine_factory, config):
46
  challenge_config = config["challenge"]
47
  max_games = challenge_config.get("concurrency", 1)
@@ -55,7 +63,7 @@ def start(li, user_profile, engine_factory, config):
55
  queued_processes = 0
56
 
57
  with logging_pool.LoggingPool(max_games+1) as pool:
58
- while True:
59
  event = control_queue.get()
60
  if event["type"] == "local_game_done":
61
  busy_processes -= 1
@@ -94,7 +102,7 @@ def start(li, user_profile, engine_factory, config):
94
  print(" Skip missing {}".format(chlng))
95
  else:
96
  raise exception
97
-
98
  control_stream.terminate()
99
  control_stream.join()
100
 
 
10
  import multiprocessing
11
  import traceback
12
  import logging_pool
13
+ import signal
14
  import time
15
  import backoff
16
  from config import load_config
 
43
  else:
44
  control_queue.put_nowait({"type": "ping"})
45
 
46
+ terminated = False
47
+
48
+ def signal_handler(signal, frame):
49
+ global terminated
50
+ terminated = True
51
+ signal.signal(signal.SIGINT, signal_handler)
52
+
53
  def start(li, user_profile, engine_factory, config):
54
  challenge_config = config["challenge"]
55
  max_games = challenge_config.get("concurrency", 1)
 
63
  queued_processes = 0
64
 
65
  with logging_pool.LoggingPool(max_games+1) as pool:
66
+ while not terminated:
67
  event = control_queue.get()
68
  if event["type"] == "local_game_done":
69
  busy_processes -= 1
 
102
  print(" Skip missing {}".format(chlng))
103
  else:
104
  raise exception
105
+ print("Terminated")
106
  control_stream.terminate()
107
  control_stream.join()
108