Re: [Tutor] is this doable
On 01Jun2019 10:02, nathan tech wrote: Now it has been mentioned, I do recall, on linux, briefly playing with psutil to retrieve memory values for a few tasks I keep running on my server. To that end, I think I now know roughly what to do. Something like this: import psutil import os path=os.getcwd()+"\\program.exe" Remark: try: r'\program.exe' instead of: "\\program.exe" because backslash escapes have meaning inside single or double quotes it is generally more reliable to use raw strings (r'') for such strings to avoid accidents. slist=[] for x in psutil.process_iter(): if(x.exe()==path): slist.append([x, x.create_time]_) r.sort() # not sure how to sort by second element, but I'd sort the list by start time list.sort() and sorted() accept an optional key= parameter which is a function that takes an element and returns the sort key. For example (untested): r.sort(key=lambda x: x[1]) to sort on x[1]. if(len(r)>1): Stylisticly we tend to just write: if r: It is a convention in Python that collections (such as lists) are "false" if they are empty. Urr, I see you said >1, not >0; this remark probably isn't relevant. But you could drop the outer brackets, not needed in Python: if len(r) > 1: It sounds like your "task" is then a running instance of "program.exe", yes? In that case the usual practive is to keep a "pid file" around with a distinctive pathname associated with the task. That is just a text file containing the process id of the task program. So rather than iterating over the process list with psutils (which will show you _other_ instances of program.exe, perhaps being run for another purpose), you just read the pid from the pid file and see if it is running. If it is, assume the task is already active. Untested incomplete example: pid_filepath = 'taskname.pid' try: pid = int(open(pid_filepath).read().strip()) os.kill(pid, 0) except (OSError, ValueError): # missing pid file, invalid contents) running = False else: running = True The "os.kill(pid, 0)" is a UNIX specific idiom for testing for a process; you can send signal 0 to a process you own successfully (the process itself never sees it); it will fail if the process doesn't exist or isn't yours. There should be a Windows equivalent for probing a process. The converse part where you start the process includes this: P = subprocess.Popen(.) # start the program with open(pid_filename, 'w') as pidf: print(P.pid, file=pidf) to update the process id file. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is this doable
Hello, Thank you for your responses. I am indeed developing for windows at the moment, with an eye casually glancing in the MAC direction as a possibility for the future that I shall think about. I'm sorry I couldn't explain better, but being only familiar with the concept in my head from one language, I wasn't quite sure what to say for each thing, how to explain it. The language is actually a bit obscure, and is used in the development of MUDs, Multi-user dungeons. I never considered it would be broken down into several modules, but that makes sense. Now it has been mentioned, I do recall, on linux, briefly playing with psutil to retrieve memory values for a few tasks I keep running on my server. To that end, I think I now know roughly what to do. Something like this: import psutil import os path=os.getcwd()+"\\program.exe" slist=[] for x in psutil.process_iter(): if(x.exe()==path): slist.append([x, x.create_time]_) r.sort() # not sure how to sort by second element, but I'd sort the list by start time if(len(r)>1): # send signal to other program to tell it to do something, either through a ntofiy app or through psutil.send_signal() # exit the program Hope I'm making more sense now, and thank you for the help everyone. Nate On 01/06/2019 04:30, Mats Wichmann wrote: > On 5/31/19 1:41 PM, nathan tech wrote: >> Hi there, >> >> So for a future project of mine, I was wondering something. >> >> Is it possible, in python, to store a running task id in the registry? >> >> I might be using the complete wrong terms here, because I'm only used to >> doing this with a specific language, but here's what I want to do: >> >> >> python mytest.py: >> >> if(registry.taskid==valid_task): >> >> print 'already open' >> >> send to open program to make a ding noise. >> >> >> I understand that the second part, the "send to program" requires the >> program to handle being sent a "wake up!" event, which is fine, it's the >> "is it already running" which I am not sure on. > there's a lot your question leaves unasked... do you want to just code > your own apps and have one be able to poke another? that's one problem, > you can define the interface yourself. Or do you want to be able to > poke arbitrary running tasks? that ends up more complicated. many > systems have notification APIs that you can make use of, some of those > are more oriented to that model (the mobile systems Android and Tizen), > some a little less but still support it (Windows - it's a more prevalent > thing in the UWP model). > > the psutil module can let you find things out about processes, might be > useful in your "is the task running" query. > > if it's okay to start processes together and it's not arbitrary, the > multiprocessing module may be of some help. > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is this doable
On 01/06/2019 00:13, Alan Gauld via Tutor wrote: > Is the language C/C++? If so you may know the OS API calls needed > and you could access those directly from Python using ctypes > That might make your job more familiar and easier. I meant to add a nod to Mark Hammond's win32 package too. It includes a process control module with access to most of the Win32 API for process control, which might be simpler than using ctypes to call the raw C API -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is this doable
On 5/31/19 1:41 PM, nathan tech wrote: > Hi there, > > So for a future project of mine, I was wondering something. > > Is it possible, in python, to store a running task id in the registry? > > I might be using the complete wrong terms here, because I'm only used to > doing this with a specific language, but here's what I want to do: > > > python mytest.py: > > if(registry.taskid==valid_task): > > print 'already open' > > send to open program to make a ding noise. > > > I understand that the second part, the "send to program" requires the > program to handle being sent a "wake up!" event, which is fine, it's the > "is it already running" which I am not sure on. there's a lot your question leaves unasked... do you want to just code your own apps and have one be able to poke another? that's one problem, you can define the interface yourself. Or do you want to be able to poke arbitrary running tasks? that ends up more complicated. many systems have notification APIs that you can make use of, some of those are more oriented to that model (the mobile systems Android and Tizen), some a little less but still support it (Windows - it's a more prevalent thing in the UWP model). the psutil module can let you find things out about processes, might be useful in your "is the task running" query. if it's okay to start processes together and it's not arbitrary, the multiprocessing module may be of some help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is this doable
On 31/05/2019 20:41, nathan tech wrote: > Is it possible, in python, to store a running task id in the registry? >From mention of the registry I assume you are running Windows? There is no registry on Unixlike systems. The answer in either case is yes since a task ID is just a number. However if the task ends the number will still be stored, so checking whether the ID refers to a live task is the trickier bit. > I might be using the complete wrong terms here, because I'm only used to > doing this with a specific language, Is the language C/C++? If so you may know the OS API calls needed and you could access those directly from Python using ctypes That might make your job more familiar and easier. Alternatively, there are OS shell commands that might work that you can call from subprocess. The os module has a bunch of functions that might help but many of them are Unix only or behave differently on Windows/Unix so you will need to study the documentation and probably experiment a bit. Things like waitpid() might work for example, but I haven't tried. Personally I'd use the shell command approach for Unix but no idea what I'd use for Windows. I suspect there may be a dynamic registry entry you can read using the winreg registry module. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is this doable
On 31May2019 19:41, nathan tech wrote: Is it possible, in python, to store a running task id in the registry? I might be using the complete wrong terms here, because I'm only used to doing this with a specific language, but here's what I want to do: python mytest.py: if(registry.taskid==valid_task): print 'already open' send to open program to make a ding noise. I understand that the second part, the "send to program" requires the program to handle being sent a "wake up!" event, which is fine, it's the "is it already running" which I am not sure on. Well, you need to have some kind of persistent storage of tasks and a way to check if some described task is running. I don't know what constitutes a task in your mind here, or what you consider "the registry". There is any number of ways to store persistent values. A simple one is a CSV file: keep one around with a line per task including the taskid and whatever other relevant information is needed. Reread the file when needed. To avoid difficulties with updating an arbitrary record in such a file (which is just a text file with lines of variying lengths) you could treat it like a log: just append more lines containing the new task state. On reading the file, just keep the last line per task. Less simple, but more flexible, might be some kind of database. Python ships with SQLite3 support, which lets you have a little SQL database in a local file. It is hard to be any more specific without knowing what you consider a task, and how you'd check if it was active. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor