Home > Enterprise >  How to allow a user a specified amount of time on a python program
How to allow a user a specified amount of time on a python program

Time:10-24

Im pretty independent when using oython since i wouldnt consider myself a beginner etc, but Iv been coding up a program that I want to sell. The problem is that I want the program to have a timer on it and when it runs out the program will no longer work. Giving the user a specified amount of time they have to use the program.

CodePudding user response:

You will want to run your program from another program using multithreading or asynchronous stuff. If you are looking for a single thing to send to your program (here, an interruption signal), then you should take a look at the signal built in package (for CPython).

(based on this answer from another post)

CodePudding user response:

If you're calling external script using subprocess.Popen, you can just .kill() it after some time.

from subprocess import Popen
from time import sleep

with Popen(["python3", script_path]) as proc:
    sleep(1.0)
    proc.kill()

Reading documentation helps sometimes.

CodePudding user response:

One way this can be done by interrupting the main thread

from time import sleep
from threading import Thread
from _thread import interrupt_main
import sys

 
TIME_TO_WAIT = 10

def kill_main(time):
    sleep(time)
    interrupt_main()
 

thread = Thread(target=kill_main, args=(TIME_TO_WAIT,))
thread.start()

try:
    while True:
        print('Main code is running')
        sleep(0.5)
except KeyboardInterrupt:        print('Time is up!')
    sys.exit()
  • Related