baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

i need an advice on how to band all types of threading forking or any kind of paralelization of my perl program. The program uses DBI to communicate with SQLite db to read and write in it. so the other day it came to me:

could i speed up my work by forking my program, give him different data to process ...

result was :

working,working,working, crash !!!!! Why -> because it came to the step where it needed to write something into the database-locked db (totally forgot that it is using databases :))

so, because i really can't remember what else could crash and am to lazy to go through whole script, i was wondering could i prevent anyone(my future self) that will ever (if ever) use this software to fork it in souch a way that if the program is already running in one location, that it cannot be started again in that location(on disc), but if the user needs to he can move the program to another location an then paralelize its work by forking for example.(why another location -> because the db's are formed in the same path where the program is ./db.db)

My idea was to create a temp file and write a flag in it, so the next time the program is started it will first check for that file and if the file is there it will exit and if it is not it means that the program is not started. and when the program exits it will delete that file. the problem is what if the exit was forced what then ... the program is not running and it will not be possible to start it because of the file(it will stay there because of the forced exit)

i hope i was clear enough in this essay of mine:)

thnx

Replies are listed 'Best First'.
Re: disable possible threads
by perreal (Monk) on Jun 15, 2009 at 13:14 UTC
    I do not think you need another state variable while SQLite already has that information. Check out this and that. Using a file for this purpose may break things in the future. Still you may store the PID of the process in that file and check if it is still around before bailing out. In that case you need to trap kill signal and the like to make things smoother. But using a file is not the best way to go.
      hi ,

      i'm aware that SQLite can be locked due to that info, but the point is to lock the whole program if there is an attempt to start the program again, and that only holds if another start of the program applies to the program in the same location. If the program is copied to another location then parallelizing it, isn't a problem.

      also i know that using a file is not the best way to go, but it is the only way that seems easy and doable :) any other more elegant solution would be a more than welcome.

      also since the location is the important factor storing PID wouldn't be of any use , would it ??????

      thank you !!

        also since the location is the important factor storing PID wouldn't be of any use , would it ??????

        Yes, you can use PID along with the signals to prevent cases when the process is forced to die. As for the location you can use the same file to store location information for running instances as well. But probably you know that already.

        You may also consider listening on a port and use sockets to request permission from the first instance. The fact that you can only listen a port once can be useful. Then you can store all information you need in a file and let the first instance control others.

Try this.
by maria66 (Initiate) on Jun 18, 2009 at 07:32 UTC
    I recommend trying the forkmanager perl module. I think it might accomplish what you're looking for. hxxp://search.cpan.org/~dlux/Parallel-ForkManager-0.7.5/ForkManager.pm For some research, maybe try these sites.
    hxxp://www.unix.org.ua/orelly/perl/prog3/ch17_01.htm
    hxxp://earth.e-raist.com/~raistlin/coding.html
    hxxp://bfr.caseywest.com/archives/005438.html
    I got that from my job of web hosting. I'm not sure of any ebooks though. I hope that helps.