def test_task_failed(self): ''' tests if a Task.FAILED is properly displayed after division by zero ''' app = Pykron() @app.AsyncRequest(timeout=0.5) def inner_empty_fun(): return 1 / 0 request = inner_empty_fun() time.sleep(0.2) assert request.task.status == Task.FAILED app.close() time.sleep(1) assert app.loop.is_running() == False
def test_return_value_wait_for_completed(self): ''' test wait_for_completed together with a callback ''' app = Pykron() @app.AsyncRequest(timeout=0.5) def inner_fun(): time.sleep(0.1) return 'test' val = inner_fun().wait_for_completed() assert val == 'test' app.close() time.sleep(1) assert app.loop.is_running() == False
def test_task_running(self): ''' tests if Task.RUNNING is properly used ''' app = Pykron() @app.AsyncRequest(timeout=3) def level0_fun(): time.sleep(2) request = level0_fun() time.sleep(0.1) # to avoid Task.IDLE assert request.task.status == Task.RUNNING app.close() time.sleep(1) assert app.loop.is_running() == False
def test_return_value_through_future(self): ''' see if the future properly stores a returned value ''' app = Pykron() @app.AsyncRequest(timeout=0.5) def inner_fun(): time.sleep(0.1) return 'test' task = inner_fun() time.sleep(0.3) assert task.future.result() == 'test' app.close() time.sleep(1) assert app.loop.is_running() == False
def test_task_succeed(self): ''' tests storing a request and checking Task.SUCCEED status ''' app = Pykron() @app.AsyncRequest(timeout=0.5) def inner_empty_fun(): time.sleep(0.1) return 1 request = inner_empty_fun() time.sleep(1) assert request.task.status == Task.SUCCEED #assert request.task.retval == 1 # TODO this fails ! app.close() time.sleep(1) assert app.loop.is_running() == False
def test_task_wait_for_completed_and_callback(self): ''' test wait_for_completed together with a callback ''' app = Pykron() @app.AsyncRequest(timeout=0.5) def inner_empty_fun(): time.sleep(0.1) return 1 def on_completed(task): assert task.status == Task.SUCCEED #assert task.retval == 1 # TODO this fails ! inner_empty_fun().wait_for_completed(callback=on_completed) app.close() time.sleep(1) assert app.loop.is_running() == False
def test_task_cancelled(self): ''' tests if Task.CANCELLED is properly used this test is limited in functionality, .cancel( ) requires specific conditions ''' app = Pykron() @app.AsyncRequest(timeout=10) def level0_fun(): for i in range(0, 90): time.sleep(0.1) request = level0_fun() time.sleep(0.1) # to avoid Task.IDLE request.cancel() time.sleep(0.1) # wait for it assert request.task.status == Task.CANCELLED app.close() time.sleep(1) assert app.loop.is_running() == False
def test_1(self): @Pykron.AsyncRequest() def like(msg): print("I like " + msg) time.sleep(0.1) return 1 a = like("strawberries") b = like("bananas") a.task.started.wait() self.assertEqual(a.task.status, Task.RUNNING) b.task.started.wait() self.assertEqual(b.task.status, Task.RUNNING) Pykron.join([a, b]) self.assertEqual(a.task.status, Task.SUCCEED) self.assertEqual(b.task.status, Task.SUCCEED) c = like("mangos") res = c.wait_for_completed() self.assertEqual(c.task.status, Task.SUCCEED) self.assertEqual(res, 1)
def test_same_time(self): ''' test if 5 functions can be run at the same time (without join) ''' app = Pykron() @app.AsyncRequest(timeout=10) def fun1(arg): time.sleep(0.5) return arg args = ['a1', 'b2', 'c3', 'd4', 'e5'] requests = list() for arg in args: requests.append((fun1(arg), arg)) time.sleep(1) # to avoid Task.IDLE for (request, arg) in requests: assert request.future.result() == arg app.close() time.sleep(1) assert app.loop.is_running() == False
def __init__(self, logging_level=Pykron.LOGGING_LEVEL, logging_format=Pykron.FORMAT, logging_file=False, logging_path=Pykron.LOGGING_PATH, save_csv=False, profiling=False): if AppConfigurator._instance != None: raise Exception("This class is a singleton!") else: AppConfigurator._instance = self AppConfigurator.pykron = Pykron(logging_level, logging_format, logging_file, logging_path, save_csv, profiling)
def test_same_time(self): ''' test if 5 functions can be run at the same time ''' @Pykron.AsyncRequest() def fun1(arg): time.sleep(0.5) return arg args = ['a1', 'b2', 'c3', 'd4', 'e5'] requests = list() for arg in args: req = fun1(arg) requests.append(req) retvals = Pykron.join(requests) for i in range(0, len(retvals)): self.assertEqual(retvals[i], args[i])
def test_create_pykron(self): app = Pykron() logger = PykronLogger.getInstance() app.close() time.sleep(1) # it takes upwards of 25ms usually to stop the loop assert app.loop.is_running() == False
def setUp(self): app = Pykron.getInstance() self.assertTrue(app.loop.is_running())
this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import time import sys sys.path.append('..') from pykron.core import Pykron app = Pykron() @app.AsyncRequest() def div(x, y): time.sleep(1) return x / y Pykron.join([div(1, 2), div(3, 2), div(1, 0), div(3, 2)])
def tearDown(self): app = Pykron.getInstance() app.close() self.assertFalse(app.loop.is_running())
''' how much time does it take to spin up a task? ''' import sys sys.path.append('..') from pykron.core import Pykron, Task, PykronLogger import time import logging import threading app = Pykron(logging_level=logging.DEBUG) logging = PykronLogger.getInstance() @app.AsyncRequest() def foo1(t0): a = time.perf_counter() - t0 logging.log.debug(a) time.sleep(0.5) t0 = time.perf_counter() mytask = foo1(t0) time.sleep(1) app.close()
modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import sys sys.path.append('..') import mod2 from pykron.core import Pykron app = Pykron(logging_path) mod2.foo2().wait_for_completed() app.close()
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from pykron.core import Pykron import pykron import time import logging import os app = Pykron() # Specifying a AsyncRequest.LOGGING_PATH will produce a logfile instead of stream app.logger.LOGGING_PATH = '.' # Decorating any Python function with AsyncRequest.decorator you will be able to # parallelize its execution thanks to ThreadPoolExecutor @app.AsyncRequest() def like(msg): print("I like " + msg) time.sleep(2) return 1 a = like("strawberries")
import sys sys.path.append('..') from pykron.core import Pykron, Task from pykron.logging import PykronLogger import time import threading app = Pykron(logging_file=True, save_csv=True) @Pykron.AsyncRequest() def foo1(): res = foo2().wait_for_completed() time.sleep(5) return 1 # A never-ending function @Pykron.AsyncRequest() def foo2(): while True: print("I am alive! ") time.sleep(1) foo3() time.sleep(2) # User-defined callback function running once the foo1 ends def on_completed(task): if task.status == Task.SUCCEED:
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ ''' Example of using StringIO to hold JSON objects for future parsing ''' from pykron.core import Pykron, PykronLogger import time from io import StringIO app = Pykron() @app.AsyncRequest(timeout=120) def fun4(): logger.log.debug("Fun 4 reporting in") time.sleep(1) #fun3() logger.log.debug("Fun 4 reporting out") time.sleep(1) output = StringIO() logger = PykronLogger.getInstance() # we manually add a FileHandler, but this is only for JSON
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import sys sys.path.append('..') from pykron.core import Pykron app = Pykron() @app.AsyncRequest() def div(x, y): return x / y res = div(1, 2) print(res)
import sys sys.path.append('..') from pykron.core import PykronLogger, Pykron app = Pykron.getInstance() @app.AsyncRequest() def foo1(): return 1