How can i dectect when a repl stops in python?

How can i dectect when a repl is stopped with python

Can you please be more specific, and provide a link to your repl?

ok here it is: pea provider idle (new game in progress!) - Replit

Can you please clarify what you mean by “detect when it is stopped”?

i am trying to make it idle so that when you log on it adds everything you would have gotten while you were offline. For this, i need to know when the repl stops so that i can record ‘time.time()’ so that i can then, when the person logs on again, find out how much time they have been offline and add what they earned.

@Firepup650 i replied

I see that, I was thinking.

would inviting u to the repl help?

Best I can say is have a thread running that is constantly updating the time, then when the repl stops you will have the time of when it stopped.

ok thanks @Firepup650

If that solves your problem, please mark my answer as the solution.

there is an issue with the code i tried to make… @Firepup650

Could you show the snippet where you made the threading?

ok here it is:

def idle():
  while True:
    a=time.time()
    b=time.gmtime(a)
    c=time.strftime("%S", b)
    with open("time.txt", "w") as f:
      f.write(c)
Thread(target=idle).start()

Does the code return an error? If so, what is the error? If not, what is it doing versus what you expect? You should probably have the code sleep for a second in-between file updates, so you don’t use excessive amounts of RAM.

it isn’t returning an error, its just not writing the items to the file everytime the loop goes

I see. I will check something and get back to you.

This writes to the file, although I don’t think it writes what you expect it to:

import time
from threading import Thread
def idle():
  while True:
    a=time.time()
    b=time.gmtime(a)
    c=time.strftime("%S", b)
    with open("time.txt", "w") as f:
      f.write(c)
    time.sleep(1)
Thread(target=idle).start()
while 1:
  time.sleep(1)
1 Like

ok i’ll try that code

that’s perfect, thank you!