[Async-sig] A tasks list and run every element again when finish

David Pineda dahalpi at gmail.com
Fri Nov 25 12:49:07 EST 2016


I answer myself. Mabe it's useful for the DOCUMENTATION:

SIMPLE SOL:

import asyncio

async def holacoro():
    print("Hola %d" % 1)
    await asyncio.sleep(1)

def renew(*args):
    task=loop.create_task(holacoro())
    task.add_done_callback(renew)


task=loop.create_task(holacoro())
task.add_done_callback(renew)

loop=asyncio.get_event_loop()

try:
     loop.run_forever()
except KeyboardInterrupt:
     print('Loop stopped')


BETTER SOL:

import asyncio
import functools

async def holacoro(v):
    print("Hola %d" % v)
    await asyncio.sleep(1)
    return v+1

#special for every coroutine
async def coromask(coro, args):
     result=await coro(*args)
     return [result]

def renew(task, coromask, coro, *args):
    print(task.result())
    task=loop.create_task(coromask(coro,task.result()))
    task.add_done_callback(functools.partial(renew, task, coromask, coro))

args=[1]
task=loop.create_task(coromask(holacoro,args))
task.add_done_callback(functools.partial(renew, task, coromask, holacoro))

loop=asyncio.get_event_loop()

try:
     loop.run_forever()
except KeyboardInterrupt:
     print('Loop stopped')

BETTER WITH 2 CORO:

import asyncio
import functools

async def holacoro(v):
    print("Hola %d" % v)
    await asyncio.sleep(1)
    return v+1

async def sumacoro(*args):
    c=sum(args)
    print("La suma es %d" %c)
    await asyncio.sleep(3)
    return c

#special for every coroutine
async def coromask(coro, args):
     result=await coro(*args)
     return [result]

#special for every coroutine
async def coromask_suma(coro, args):
     _in=[args[-1]]
     result=await coro(*args)
     _in.append(result)
     return _in

def renew(task, mask, coro, *args):
    result=task.result()
    task=loop.create_task(mask(coro,result))
    task.add_done_callback(functools.partial(renew, task, mask, coro))

loop=asyncio.get_event_loop()

args=[1]
task1=loop.create_task(coromask(holacoro,args))
task1.add_done_callback(functools.partial(renew, task1, coromask, holacoro))

args2=[1,2]
task2=loop.create_task(coromask_suma(sumacoro,args2))
task2.add_done_callback(functools.partial(renew, task2, coromask_suma,
sumacoro))


try:
     loop.run_forever()
except KeyboardInterrupt:
     print('Loop stopped')



2016-11-23 11:53 GMT-03:00 David Pineda <dahalpi at gmail.com>:

> Hello.
>
> I'm using the asyncio module. I have a list with different task, every
> tasks has different time to be done.
>
> In a figure:
>
> fn 1 -> -> -> ->
> fn 2 --> --> --> --> -->
> .
> .
> .
> fn N -----> -----> -----> ----->
>
> So i need to repeat every task when its done.
>
> I tried to use 'while' but while have to complete all list first to repeat
> the list, this is not useful for me.
>
> Y ask you, fellows, about if there are a way to do that. In particular
> these tasks are with socket and communication methods. I ve tried
> with multiprocess but the broke because are different loop events. Yes, i
> tryyo mix asyncio with multiprocess, but in this case didn't work
>
> I will be very thankfull of you.
>
> Best Regards!
>
>
> --
> David A. Pineda Osorio
> F:+56 9 82142267
> Ingeniero Civil Electricista
> Universidad de Chile
>



-- 
David A. Pineda Osorio
F:+56 9 82142267
Ingeniero Civil Electricista
Universidad de Chile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20161125/273e2afa/attachment.html>


More information about the Async-sig mailing list