feat(bot): ✨ Move messages flushing to a separate process.
This commit is contained in:
@@ -18,6 +18,14 @@ def shard(shard_id: int, shard_count: int) -> None:
|
||||
bot.run(config.TOKEN)
|
||||
|
||||
|
||||
def flush_process():
|
||||
import asyncio
|
||||
|
||||
from procs.flush_messages import loop
|
||||
|
||||
asyncio.run(loop())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import multiprocessing as mp
|
||||
|
||||
@@ -61,17 +69,29 @@ if __name__ == "__main__":
|
||||
ps: list[mp.Process] = [
|
||||
mp.Process(target=shard, args=(i, shard_count)) for i in range(shard_count)
|
||||
]
|
||||
flushing_p = mp.Process(target=flush_process)
|
||||
|
||||
for i, p in enumerate(ps):
|
||||
p.start()
|
||||
|
||||
print(
|
||||
f"Shard {i}'s process started." + (" Waiting 5 seconds start initialize next shard..." if len(ps) - 1 < i else " All shards were started.")
|
||||
f"Shard {i}'s process started."
|
||||
+ (
|
||||
" Waiting 5 seconds start initialize next shard..."
|
||||
if len(ps) - 1 < i
|
||||
else " All shards were started."
|
||||
)
|
||||
)
|
||||
|
||||
if i + 1 != len(ps):
|
||||
time.sleep(5)
|
||||
|
||||
print("Starting messages flushing process...")
|
||||
|
||||
flushing_p.start()
|
||||
|
||||
print("Messages flushing process started.")
|
||||
|
||||
try:
|
||||
while True:
|
||||
for i in range(shard_count):
|
||||
@@ -86,6 +106,14 @@ if __name__ == "__main__":
|
||||
)
|
||||
time.sleep(5)
|
||||
|
||||
if not flushing_p.is_alive():
|
||||
print("Messages flushing process was stopped. Restarting it...")
|
||||
|
||||
flushing_p = mp.Process(target=flush_process)
|
||||
flushing_p.start()
|
||||
|
||||
print("Messages flushing process started.")
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
@@ -95,9 +123,15 @@ if __name__ == "__main__":
|
||||
p.terminate()
|
||||
print(f"Terminated shard {i}.")
|
||||
|
||||
print("Stopping messages flushing process...")
|
||||
|
||||
flushing_p.terminate()
|
||||
|
||||
print("Checking that all processes have finished...")
|
||||
|
||||
|
||||
for p in ps:
|
||||
p.join()
|
||||
|
||||
flushing_p.join()
|
||||
|
||||
print("All processes have been terminated.")
|
||||
|
||||
Reference in New Issue
Block a user