killing a long running statement

This is the place for queries that don't fit in any of the other categories.

killing a long running statement

Postby metulburr » Mon Sep 02, 2013 9:22 am

Code: Select all
import signal

class Alarm(Exception):
    pass

def alarm_handler(signum, frame):
    raise Alarm


signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(3)  # seconds
try:
    while True:
        print('in loop')
    #print('proceed after loop')
    #a =  12345678 ** 123456789
    signal.alarm(0)  # reset the alarm
except Alarm:
    print("Oops, taking too long!")
    # whatever else


this code will terminate a process (in unix) for an infinite loop, or long running function, etc., but my goal was to prohibate someone from entering something like:
Code: Select all
12345678 ** 123456789


So the preceeding code cuts the infinite while loop out after 3 seconds, but if uncomment the exponent cray thing, it freezes and does not cut it after 3 seconds. Why?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: killing a long running statement

Postby Mekire » Mon Sep 02, 2013 9:28 am

I was actually just wondering about this. I haven't actually tried to implement it but I was wondering if multiprocessing might work. Run the potentially offending code in a separate process/thread and kill it if it is taking too long.

-Mek
User avatar
Mekire
 
Posts: 1031
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: killing a long running statement

Postby metulburr » Mon Sep 02, 2013 9:31 am

I was thinking of trying subprocess to execute the same exponent string with eval, then attempt to kill that process. In normal situations this may work, But my problem is I dont think i can do that, as I am simplifying expressions using sympy module. So eval is evaluating the string which has the sympy object of the expression in this current namespace. The same expression in another process would result in a NameError. I dont know, it just seemd more simple to kill the signal than make a ton of code to pass the sympy objects off. But maybe i might have to do that as this apparently does not work.

but the same worked with an infinite while loop, it seemd weird that it would kill that, but not something like:
Code: Select all
12345678 ** 123456789


EDIT:
that and i am not the greatest at threading and multiprocessing still

EDIT2:
or i was thinking something really simple. Like loop the input searching for exponents (since thats the only thing i am having problems with) and deny use to exponents being greater than (oh lets say) 4 digits. Which would allow me to skip multiprocess/threading/signals altogether.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: killing a long running statement

Postby Mekire » Mon Sep 02, 2013 10:36 am

I was thinking something like this, though also I really don't have much experience with threading/multiprocessing:

Code: Select all
import multiprocessing
import time
from queue import Empty

def pow_it(x,y,que):
    que.put(x**y)

if __name__ == '__main__':
    my_queue = multiprocessing.Queue()
    process = multiprocessing.Process(target=pow_it, args=(2000000,2000000,my_queue))
    process.start()
    process.join(3.0)
    if process.is_alive():
        print("Terminating process.")
        process.terminate()
        time.sleep(0.1)
    try:
        print(my_queue.get(timeout=0.1))
    except Empty:
        print("Process failed to finish.")
    input("Enter to exit:")

Seems to work. If the args passed to the process are reasonable it will spit out the answer. If not it will stop the process after 3 seconds (as should be the case here passing (2000000,2000000).
-Mek
User avatar
Mekire
 
Posts: 1031
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: killing a long running statement

Postby metulburr » Mon Sep 02, 2013 1:33 pm

that worked very well. I had to tinker with it a bit to get it to work the way i needed, but essentially i was worrying about other stuff that was irrelevant to multiprocessing.

thanks again.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests

cron