Skip to content

Commit

Permalink
Added AyncReq timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ddetommaso committed Apr 20, 2020
1 parent da38feb commit d60e5a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
20 changes: 11 additions & 9 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class Agent:
def callback(self, arg):
print(arg)

@AsyncRequest.decorator
@AsyncRequest.decorator()
def like(self, msg):
msg = "I like " + msg
time.sleep(3)
print(msg)
return 1

@AsyncRequest.decorator
@AsyncRequest.decorator()
def see(self, msg):
msg = "I see " + msg
time.sleep(1)
Expand All @@ -61,22 +61,24 @@ def bye(self):
AsyncRequest.join([a,b])
jojo.bye()

def callback(task):
print(task)

@AsyncRequest.decorator

@AsyncRequest.decorator()
def foo1():
time.sleep(1)
print("foo1")
return 1

@AsyncRequest.decorator
@AsyncRequest.decorator(timeout=1.0)
def foo2():
time.sleep(2)
print("foo2")
return 2


foo1().wait_for_completed()
try:
foo2().wait_for_completed(timeout=1.0)
except:
print("A timeout occurs!")
a = foo1().wait_for_completed(callback=callback)
b = foo2().wait_for_completed(callback=callback)

AsyncRequest.join([a,b])
37 changes: 19 additions & 18 deletions pykron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@

class Task:

IDLE = 'IDLE',
RUNNING = 'RUNNING',
SUCCEED = 'SUCCEED',
FAILED = 'FAILED',
IDLE = 'IDLE'
RUNNING = 'RUNNING'
SUCCEED = 'SUCCEED'
FAILED = 'FAILED'
TIMEOUT = 'TIMEOUT'

def __init__(self, target, args, name=None):
def __init__(self, target, args, timeout, name=None):
self._target = target
self._args = args
self._timeout = timeout
self._name = name
self._retval = None
self._status = Task.IDLE
Expand Down Expand Up @@ -102,14 +103,14 @@ def status(self):
def start_ts(self):
return self._start_ts

def run(self, timeout=10.0):
def run(self):
self._start_ts = time.perf_counter()
self._status = Task.RUNNING

with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
try:
future = executor.submit(self._target, *self._args)
t = future.result(timeout=timeout)
t = future.result(timeout=self._timeout)
self._retval = t
self._status = Task.SUCCEED
except concurrent.futures.TimeoutError:
Expand All @@ -125,14 +126,14 @@ def run(self, timeout=10.0):
class AsyncRequest:

@staticmethod
def decorator(foo):

def f(*args, **kwargs):
task = Task(foo, args, foo.__name__)
req = AsyncRequest(task)
return req

return f
def decorator(timeout=10.0):
def wrapper(foo):
def f(*args, **kwargs):
task = Task(foo, args, timeout, foo.__name__)
req = AsyncRequest(task)
return req
return f
return wrapper

@staticmethod
def join(requests, timeout=None):
Expand Down Expand Up @@ -163,11 +164,11 @@ def __init__(self, task):
def future(self):
return self._future

def wait_for_completed(self, callback=None, request_timeout=None):
def wait_for_completed(self, callback=None):
if not callback is None:
self.on_completed(callback)
res = self.future.result(request_timeout)
return res
#self.future.result()
return self

def on_completed(self, callback):
self._callback = callback
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@

setup(
name='pykron',
version='0.3',
version='0.4',
description='Python module for managing asynchronous tasks',
url='https://github.com/s4hri/pykron',
download_url='https://github.com/s4hri/pykron/archive/0.3.tar.gz',
download_url='https://github.com/s4hri/pykron/archive/0.4.tar.gz',
author='Davide De Tommaso',
author_email='dtmdvd@gmail.com',
keywords=['multithreading'],
Expand Down

0 comments on commit d60e5a3

Please sign in to comment.