0

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.

Vibez
  • 1
  • Does this answer your question? [Run certain code every n seconds](https://stackoverflow.com/questions/3393612/run-certain-code-every-n-seconds) – President James K. Polk Oct 23 '22 at 21:01
  • If I understand your question correctly (which would mean everybody else here so long didn't), you would like to give some sort of trial period!? Beware that people can see your code and change it. So, you can of course use the `datetime` module, but would need to hide the code, e.g. using `pyInstaller` to keep at least amateur hackers away from cracking it. – Dr. V Oct 23 '22 at 21:14

3 Answers3

0

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)

Biskweet
  • 130
  • 1
  • 9
0

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.

hkc
  • 61
  • 1
  • 4
0

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()
Jacrac04
  • 16
  • 5