Skip to content

Commit

Permalink
task results in wait_completed
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Sep 6, 2019
1 parent 4b83a09 commit 7e5c27e
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 30 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ task_supervisor.stop()
### Background task

```python
from atasker import background_task, TASK_LOW, TASK_HIGH
from atasker import background_task, TASK_LOW, TASK_HIGH, wait_completed

# with annotation
@background_task
def mytask():
print('I am working in the background!')
return 777

mytask()
task = mytask()

# optional
result = wait_completed(task)

print(task.result) # 777
print(result) # 777

# with manual decoration
def mytask2():
print('I am working in the background too!')

background_task(mytask2, priority=TASK_HIGH)()
task = background_task(mytask2, priority=TASK_HIGH)()
```
### Async tasks

Expand All @@ -120,10 +127,12 @@ async def calc(a):

# call from sync code

# put coroutine and forget
background_task(calc)(1)
# put coroutine
task = background_task(calc)(1)

wait_completed(task)

# get coroutine result
# run coroutine and wait for result
result = a1.run(calc(1))
```

Expand Down
2 changes: 1 addition & 1 deletion atasker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

from atasker.supervisor import TaskSupervisor
from atasker.supervisor import TASK_LOW
Expand Down
2 changes: 1 addition & 1 deletion atasker/co.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

from atasker import task_supervisor

Expand Down
2 changes: 1 addition & 1 deletion atasker/f.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

import traceback
import threading
Expand Down
2 changes: 1 addition & 1 deletion atasker/supervisor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

import threading
import multiprocessing
Expand Down
16 changes: 10 additions & 6 deletions atasker/threads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

import threading
import time
Expand All @@ -16,7 +16,7 @@
from atasker import TASK_NORMAL
from atasker import TT_THREAD, TT_MP, TT_COROUTINE

from atasker.supervisor import ALoop
from atasker.supervisor import ALoop, Task


class LocalProxy(threading.local):
Expand Down Expand Up @@ -205,13 +205,17 @@ def _background_task_thread_runner(f, supervisor, task_id, *args, **kwargs):


def wait_completed(tasks, timeout=None):
'''
raises TimeoutError
'''
t_to = (time.time() + timeout) if timeout else None
for t in tasks:
for t in [tasks] if isinstance(tasks, Task) else tasks:
if timeout:
t_wait = t_to - time.time()
if t_wait <= 0: return False
if t_wait <= 0: raise TimeoutError
else:
t_wait = None
if not t.completed.wait(timeout=t_wait):
return False
return True
raise TimeoutError
return tasks.result if isinstance(tasks,
Task) else [x.result for x in tasks]
2 changes: 1 addition & 1 deletion atasker/workers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

import threading
import logging
Expand Down
7 changes: 5 additions & 2 deletions doc/tasks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,8 @@ You may wait until pack of tasks is completed with the following method:
wait_completed([task1, task2, task3 .... ], timeout=None)
The method return *True* if all tasks are finished, or *False* if timeout was
specified but some tasks are not finished.
The method return list of task results if all tasks are finished, or raises
*TimeoutError* if timeout was specified but some tasks are not finished.

If you call method with a single task instead of list or tuple, single result
is returned.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.1"
__version__ = "0.4.2"

import setuptools

Expand Down
2 changes: 1 addition & 1 deletion tests/mp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

def test(*args, **kwargs):
print('test mp method {} {}'.format(args, kwargs))
Expand Down
2 changes: 1 addition & 1 deletion tests/mpworker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

from atasker import BackgroundIntervalWorker

Expand Down
16 changes: 8 additions & 8 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__author__ = "Altertech Group, https://www.altertech.com/"
__copyright__ = "Copyright (C) 2018-2019 Altertech Group"
__license__ = "Apache License 2.0"
__version__ = "0.4.1"
__version__ = "0.4.2"

from pathlib import Path

Expand Down Expand Up @@ -264,19 +264,19 @@ def test_result_thread(self):
def t1():
return 777

t = background_task(t1)()
wait_completed([t])
self.assertEqual(t.result, 777)
def t2():
return 111

task1 = background_task(t1)()
task2 = background_task(t2)()
self.assertEqual(wait_completed((task1, task2)), [777, 111])

def test_result_mp(self):

from mp import test2

t = background_task(test2, tt=TT_MP)()
wait_completed([t])
self.assertEqual(t.result, 999)


self.assertEqual(wait_completed(t), 999)

def test_aloop_run(self):

Expand Down

0 comments on commit 7e5c27e

Please sign in to comment.