Comments (8)
Working but dirty example:
import sys
from threading import Timer
import time
# import thread # python 2
import _thread as thread # python 3
from contextlib import contextmanager
@contextmanager
def timeout(t):
try:
yield Timer(t, thread.interrupt_main).start()
except KeyboardInterrupt:
raise TimeoutError
def forever():
while 1:
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
try:
with timeout(3):
forever()
except TimeoutError:
print('timeout')
from timeoutcontext.
Added KeyboardInterrupt disambiguation:
import sys
from threading import Timer
import time
import _thread as thread
from contextlib import contextmanager
@contextmanager
def timeout(t):
try:
timer = Timer(t, thread.interrupt_main)
timer.daemon = True
yield timer.start()
except KeyboardInterrupt:
if timer.is_alive():
raise
raise TimeoutError
def forever():
while 1:
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
try:
with timeout(3):
forever()
except TimeoutError:
print('timeout')
from timeoutcontext.
@vvscloud the above snippet might interrest you.
from timeoutcontext.
Hi AntoineCezar,
I wrote a sample snippet which works on windows (Tested it as well); Please validate from your end and share feedback if any.
Hope it helps.
def run(args, cwd=None, shell=False, kill_tree=True, timeout=-1, env=None):
from threading import Timer
p = subprocess.Popen(args, shell=shell, cwd=cwd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env)
if timeout != -1:
timer = Timer(timeout, p.kill)
timer.start()
stdout, stderr = p.communicate()
if timeout != -1:
timer.cancel()
return p.returncode, stdout, stderr
from timeoutcontext.
Hi @vvscloud,
I've tested your snippet and it works like a charm. I can't figure out how to make it work in timeoutcontext has it would require sending the context to a different process (probably with multiprocessing instead of subprocess). However it's a nice way to timeout shell processes.
Have you tried the thread based snippet? I do not have any Window for the moment. Your feedback would confirm that it's a valid option.
from timeoutcontext.
Working thread based solution that does not send your code to another thread: https://github.com/AntoineCezar/timeoutcontext/tree/thread_timeout
Before I release this, I must:
- Test it on Windows (that I don't have right now)
- Update the README
- Ask for experts whether my solution is not dangerous (who?)
from timeoutcontext.
I can test on windows. Please let me know.
from timeoutcontext.
@vvscloud you can test the thread_timeout branch it will automatically use threading on Windows
from timeoutcontext.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timeoutcontext.