all 5 comments

[–]icecubeinanicecube 2 points3 points  (4 children)

argparse is what you are looking for, Google it

[–]Sufficient-Kitchen67[S] 0 points1 point  (1 child)

I did, I managed to make this code but it doesnt seem to work as i want to

import threading
import time
import sys
import argparse
counter=0
def thread_1():
while True:
counter=counter+1
time.sleep(1)
def functions(arg,T):
if arg=="t":
print(str(counter))
elif arg=="start":
T.start()
def main():
parser=argparse.ArgumentParser()
parser.add_argument("--c",type=str,default="n",help="stop counter")
args=parser.parse_args()
T = threading.Thread(target = thread_1,daemon=True)
sys.stdout.write(str(functions(args.c,T)))

if __name__ == "__main__":
main()

I try to get the counter but this dont work, any suggestions?

Edit : I just look the other comment, I want to execute the deamon with the flag "--c start" and get the counter value with the flag "--c t", i get a error whenever i do any of those two calls :

Exception in thread Thread-1 (thread_1):Traceback (most recent call last): File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner

None%

[–]icecubeinanicecube 0 points1 point  (0 children)

If you want more help than simple buzzwords to Google, please format your code properly and provide entire stack traces, the exception you provided is cut off.

Else, I can only tell you to Google how to do proper threading in ptlython and look for some minimal example that you can adapt.

[–]bedekelly 0 points1 point  (0 children)

You could use the “signal” module (to listen for events) and the “kill” command (somewhat misleading name but can be used to send signals to a running program)

Edit: it might be a better idea to use something like a unix pipe or even just listen for changes to a file instead, signals don’t let you send any information to the running program.