Giter Club home page Giter Club logo

Comments (8)

AntoineCezar avatar AntoineCezar commented on July 21, 2024

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.

AntoineCezar avatar AntoineCezar commented on July 21, 2024

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.

AntoineCezar avatar AntoineCezar commented on July 21, 2024

@vvscloud the above snippet might interrest you.

from timeoutcontext.

vvscloud avatar vvscloud commented on July 21, 2024

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.

AntoineCezar avatar AntoineCezar commented on July 21, 2024

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.

AntoineCezar avatar AntoineCezar commented on July 21, 2024

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.

vvscloud avatar vvscloud commented on July 21, 2024

I can test on windows. Please let me know.

from timeoutcontext.

AntoineCezar avatar AntoineCezar commented on July 21, 2024

@vvscloud you can test the thread_timeout branch it will automatically use threading on Windows

from timeoutcontext.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.