473,378 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Python process automatically restarting itself

What is the best way for a Python process (presumed to be a script run
by the interpreter binary, not embedded in some other program) to
restart itself? This is what I've been trying:

import __main__

for path in sys.path:
path += '/' + __main__.__file__
if os.access(path, os.F_OK): break
else:
raise Exception('WTF?')

#Then, something like...
os.execl(sys.executable, sys.executable, path, *sys.argv[1:])

BUT! This is a multi-threaded program, and at least on OS X, trying to
exec*() in a threaded process raises OSError [Errno 45] Operation not
supported. So I tried having it fork, and having the child process
wait for the parent process to die (in various ways) and then exec*().
That seemed to work, sort of, but the child would not be attached to
the original terminal, which is a must.

Any ideas?

Oct 11 '07 #1
1 6718
On Oct 11, 7:17 am, Adam Atlas <a...@atlas.stwrote:
What is the best way for a Python process (presumed to be a script run
by the interpreter binary, not embedded in some other program) to
restart itself? This is what I've been trying:

import __main__

for path in sys.path:
path += '/' + __main__.__file__
if os.access(path, os.F_OK): break
else:
raise Exception('WTF?')

#Then, something like...
os.execl(sys.executable, sys.executable, path, *sys.argv[1:])

BUT! This is a multi-threaded program, and at least on OS X, trying to
exec*() in a threaded process raises OSError [Errno 45] Operation not
supported. So I tried having it fork, and having the child process
wait for the parent process to die (in various ways) and then exec*().
That seemed to work, sort of, but the child would not be attached to
the original terminal, which is a must.

Any ideas?
sys.path is the search path that Python uses for finding modules, and
probably isn't what you wanted. Also, it is bad practice to hard code
the file separator. In general you will want to do this:

import os.path
path = os.path.join(path, filename)

which will automatically insert the correct separator for the os you
are using.

That is moot though, since I think this is a better solution:

import os.path, __main__
path = os.path.abspath(__main__.__file__)

That isn't really necessary though. In fact, I think restarting the
process using exec* is the wrong way to go. I would simply encapsulate
my program inside of a function, set a restart flag, return from said
function and restart if the flag was set.

Something like this:

restart = False
def main():
# do my stuff

if need_to_restart:
global restart
restart = True

#do clean-up (shut down threads, etc.)
return retval

if __name__ == "__main__":
retval = main()
while restart:
restart = False
retval = main()
sys.exit(retval)

I suppose there are reasons that you might want to restart the
process. Most of the time you are going to want to do something like
what I suggested though. And even in those other cases, you probably
want to use the subprocess module instead of an exec* function.

Matt

Oct 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Hunsley | last post by:
I'm running a monitoring script under linux written in python. It's nohup'ed so that I can log out and it will continue running happily and so on, but sometimes I need to kill the script before...
2
by: Michele Simionato | last post by:
Is there a way to send a SIGINT/KeyboardInterrupt to a Python process (knowing the pid) that works both on Unix and Windows? Michele Simionato
8
by: MackS | last post by:
Hello! This question does not concern programming in python, but how to manage python processes. Is there a way to "name" a python process? At least on Linux, if I have two python programs...
0
by: Shanon | last post by:
Hi, I would to know if there're some way to have a dump of all the threads started by a python process. I want to see the TID corresponding of each thread because I need them to get the CPU time...
1
by: johnny | last post by:
What I want to do is the following: Web user uploads a word doc, and I need it to move the uploaded word doc, on to another machine and conver it to pdf. Then update the database and allow...
4
by: yossi.kreinin | last post by:
Hi! Is there a way to save the state of a Python process for later inspection with a debugger? One way to do this is to dump core, but is the result usable for debugging with pdb (it can be...
4
by: Dan Stromberg | last post by:
I wrote a script(1) replacement in python (http://stromberg.dnsalias.org/ ~dstromberg/pypty/), but I'm encountering a problem in it. I think I know the solution to the problem, but I'd've thought...
9
MusoFreak200
by: MusoFreak200 | last post by:
hello i am creating a website for both my school assignment and to really upload. could someone help me with the following question, please? how do you make your background image...
1
by: WayneClements | last post by:
I have an old MS Access 2007 project and I have moved some of its procedures to VB.NET using VS 2008. I have compiled the DLL and MS Access can use it successfully. But when I exit MS Access it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.