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 ''' @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])
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") b = like("bananas") app.join([a, b]) c = like("mangos").wait_for_completed() app.close()
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)])