[Tutor] could someone please explain multiprocessing
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Jul 5 13:34:36 EDT 2020
On 05/07/2020 11:33, nathan tech wrote:
> import psutil
> currently_running=[]
> for process in running_processes:
> if(process["name"]=="my_programs_launcher" or
> process["name"]=="my_programs_name"):
>
> running.append(process["id"])
> if(len(running)==1): # we are the only one! Launch the main program and job done.
I think you are doing more work than necessaary. You don;t need a list
of running instances since there is only zero or one. - Its a boolean
condition. So just use the if test above.
> else:
> # we are already running. Open the shared file, append the arguments,
> and then send a signal
Yep.
> Main program:
> # do all the things
> # check to see if any signals have been received
> if(signal_received):
> # open the file and process it, deleting and or clearing the file
> after so that it does not repeat itself.
Yep. But... check out the signal module.
The actual code will be more event driven than the above.
You just set up the handler and the OS will work with the
Python virtual machine to call the handler when the signal
arrives.
> if(file_last_modified(path)>last_time_this_program_accessed_it):
> # process contents and update variables
Yep again.
--
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
More information about the Tutor
mailing list