-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 El 08/11/11 23:17, Jose Caballero escribió: > Hola, > > > pregunta algo complicada de expresar, pero mas o menos el 'subject' lo > dice todo. > Estoy buscando una forma de poder matar la ejecucion de funciones y/o > metodos de clases cuando se produce un timeout. > He buscado algo en google, y hay algunas soluciones interesantes (como > usar signal), pero ninguna me llega a funcionar del todo. > > Mis dos problemas fundamentales son: > > - tiene que funcionar con python2.4 standard, sin librerias 3rd-party > - tiene que funcionar incluso cuando el metodo a matar pertenece a una > clase heredada de threading.Thread. > > El segundo requisito imposibilita usar signal. > Me ha soltado un contundente: > > ValueError: signal only works in main thread > > > He intentado tambien ejecutar la funcion/metodo en otro thread, y pasar > el valor del timeout a join(). > Sin embargo eso no parece matar la ejecucion de codigo en cuestion. > Efectivamente, parece que se llama a join() antes de que la ejecucion > termine, pero esta sigue su camino en el background, no parece que > join() realmente la haya matado. > > Alguien conoce una solucion para matar (de verdad) la ejecucion de > funciones y metodos, compatible con threads?
Una solución tonta, pero que igual te sirve, sería convertir el thread en un daemon. Usando un hilo auxiliar, sería algo así: import threading def run_limited(time_limit, thr): def runwrap(): thr.daemon=True thr.start() thr.join(time_limit) if thr.isAlive(): print "Forzando salida" t=threading.Thread(target=runwrap) t.start() t.join() class NonStopThread(threading.Thread): def run(self): while True: pass t=NonStopThread() run_limited(10,t) - -- Hyperreals *R: http://ch3m4.org/blog Quarks, bits y otras criaturas infinitesimales -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOuyUgAAoJEFdWyBWwhL4FjUgH/1xkp+HtEk2n6IWR0La3Hnig jHVVbqIME+LBrzQOa8t0yo6oW74azyZxjd3SblwNsRqjopnxszBFxgQzxPZdUDcA m6byBGVHwCSeTtLSegIhfqgtrYr8MacYrPxALcdcC9j9GYT8ewB038tM2aS7BAHY ctQK53//R2ZlKt51mSskde/877XeVW8flcKHWCrZ0PvMKiko53ssC/ggzihYr0pp Hn7gmf+hnGLSimjIlcc9AdQBIcq6I65oPmeHTWH1EKtEMckJLnuFi2BFKas9zuo6 SPreLlat/dJMaged9it0v8yCZsp057YtGGOqCTrJ97kmJ3UwcdkM105/8JjXGOI= =LPxV -----END PGP SIGNATURE----- _______________________________________________ Python-es mailing list Python-es@python.org http://mail.python.org/mailman/listinfo/python-es FAQ: http://python-es-faq.wikidot.com/