Reputation: 705
I'm working on an evolutionary computing problem, which I'm implementing with the excellent ECSPY module. The fitness value I'm using is derived from a pretty complex dynamics simulation. Thing is that I do not like the approach of making my simulation bomb-proof; its pretty useless since the evolutionary process could come up with situations that the simu engine just is not built to be able to solve. However, constraining the generator to return scenes that are solvable is over constraining things.
So my approach is simple; if the simulation takes too long, or crashes, well, I'll just let Darwin's mercy handle it.
I'm using the multiprocessing module to evaluate the fitness of the candidates. How do I catch a segfaulting interpreter or kill it given a number of seconds?
Many thanks in advance,
-jf
Upvotes: 3
Views: 256
Reputation: 391846
Use subprocess
to "wrap" a Python interpreter inside a Python script.
Start a Python interpreter that runs your thing.
Start a clock.
Wait until the clock runs out or the child process crashes.
The easy, lazy way to do this is to poll the subprocess periodically to see if it's dead yet. Yes, it's "busy waiting", but it's simple to implement and relatively low resource cost if you don't want instant notification when the subprocess finishes.
import subprocess
import time
timeout = # your timeout interval
real_work= subprocess.Popen( "python the_real_work.py" )
start= time.time()
status= real_work.poll()
while time.time()-start < timeout and not status:
time.sleep( 10.0 )
status= real_work.poll()
if not status:
real_work.kill()
Something like that might work out. It has a race condition if it happens to exit right at the timeout interval; the kill could fail.
Upvotes: 3