Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Release 0.5
David Pineda
6 Code’s Documentation 17
6.1 The Async While to be a Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 The Scheduler explained Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.3 The Assignator role . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7 To do 23
Index 27
i
ii
Async Task Tools Documentation, Release 0.5
The Tasktools module allows you to work with the asyncio library from the programming language Python on the
versions above the 3.5.
The features which this module provides is the ability to create async functions that runs in an async while in first
case. The problem is when you have a lot of Task to run at same time (a lot or many) you have to order your mind and
design a system or machine using more resources.
To do that, you have to activate multiple processes and create the tasks for every active process. In that stage the
TaskScheduler and TaskAssignator are useful. Like its names says are specific machines to schedule tasks on the
procesess and activate with the assignator.
Hence, when you have a system that needs to run permanently, check new data, convert and send to other place is
convenient to study this module and implement your functions and classes using the tools from this module.
¡Enjoy!
Table of Contents: 1
Async Task Tools Documentation, Release 0.5
2 Table of Contents:
CHAPTER 1
An async function or coroutine is an special kind of function which allows you to work with events that happend
without your control but you need to access to some of the information generated on that.
For example, if you realize a query to a server you can’t control how much time last the work of the computer searching
the data and returning to you, so you have to await for the data. But, what can you do with this awaited time. In the
classic paradigm you have to wait for the returning data, but with the asynchronous paradigm you can do something
else.
With that in mind you can use this tools to message exchange in any case, socket communications (to databases, other
servers, etc) and any system with multiple events happening at same time.
In Python the library async provides an implementation for that purpose, and you have to consider some new concepts
and stages to enable the execution of this coroutines.
The classic way or synchronous programming way, you have a number of actions that the computer runs one after
other, you can’t attend other events.
In python the GIL(Global Interpreter Locking) take charge of that and blocks any other action while the code is
running.
With that programming style we can say the we are near reality, on our programs we depends from things that can
or can’t happend without our control, but we can give some confidence to some systems or the systems that we know
3
Async Task Tools Documentation, Release 0.5
how works (in general), and then give or send, and get or receive data from them.
So, when we detect that something is ready to proceed, we continue with that. The order whem that happen doesn’t
matter.
Better explained:
The DavidBeaz site offer you a wide variety of examples, videos and material about the most interesting subjects on
python and computer science. Put in clear a lot of concepts of complex things like the asyncio module.
The WillysCave site explains the main differences betweeon classic and asynchronous programming ways.
The MichaelKuty presentation to explain the evolution of this subjects.
The DavidPineda, yes me, in español with different and useful examples
The Modules consider an interesting list of different uses of asyncio library.
1 import asyncio
2
loop = asyncio.get_event_loop()
#creamos tarea y la asociamos al loop, ejecutandola
loop.run_until_complete(holacoro())
future_task = holacoro()
future_task_2 = othercoro()
tasks_gathered = asyncio.gather(*tasks)
loop.run_until_complete(tasks_gathered)
1. The eventloop with many different implementations, the standar and others (like quamash for qt)
2. Abstractions for layers Transport and Protocol, (OSI model?)
3. Support for networking uses: TCP, UDP, UnixSocket, SSL, subprocesses, pipes, etc.
4. Sintaxis based on (async, await) pair.
5. Manage Futures and coroutines
6. Interface to work with threads and multiprocessing
If you want to work with the last modifications, when the project is updated, you can clone and install in dev mode
(on the terminal).
If you want to work with the stable release you can install with pip in your virtualenv .
wget https://files.pythonhosted.org/packages/05/31/
˓→ee3981f7c4d6ee4968c53e40e0ed77089c50861cbe1960553006d0923a3f/tasktools-0.5.tar.gz
7
Async Task Tools Documentation, Release 0.5
If you need to run your coroutines and each must be executed independent, like receive stream data from a list of
sources, in which every source have different frecuencies, you need this.
TaskLoop provides a set of functions and asyncio coroutines who can help you on your mission.
You need
fn1 ->->->->->->->-> |
fn2 —>—>—>—> |
.|
.|
.|
fnN –>–>–>–>–>–> |
You can’t do a /for/ and obtain good results. You need free tasks.
So, the model you need is for every source-coroutine is:
9
Async Task Tools Documentation, Release 0.5
Args input
First task scheduled
Call when done
Define
Your Coroutine object Renew task when done Task(k)
Call when done
Function Inputs Do
coromask [coro, args, fargs] await coro
renew [task, coro, fargs,*args] renew task
simple_fargs [input, output] return input
simple_fargs_out [input, output] return output
The following code is an example about how to use the taskloop methods, we have two coroutines, define a fargs for
1 import asyncio
2 import functools
3
22 #every coroutine
23 async def coromask(coro, args, fargs):
24 _in=args
25 obtained=await coro(*args)
26 result=fargs(_in, obtained)
27 return result
28
34 loop=asyncio.get_event_loop()
35
36 args=[1]
37 task1=loop.create_task(coromask(holacoro,args,fargs_holacoro))
38 task1.add_done_callback(functools.partial(renew, task1, holacoro, fargs_holacoro))
39
40 args2=[1,2]
41 task2=loop.create_task(coromask(sumacoro,args2,fargs_sumacoro))
42 task2.add_done_callback(functools.partial(renew, task2, sumacoro, fargs_sumacoro))
43
44
45 try:
46 loop.run_forever()
47 except KeyboardInterrupt:
48 print('Loop stopped')
Now, those async functions are encapsulated on the Tasktools module inside the taskloop file. Hence, to call them you
have to use the tradicional python system to use modules and functions.
11
Async Task Tools Documentation, Release 0.5
The needs to create a system that attends multiples sources at same time produce the implementation of the TaskSched-
uler and TaskAssignator classes that provide the feature of work as a general conceptualization of a big machine with
multiple tasks.
For a specific implementation, for your goal, you have to inherit the Scheduler and implement this funcions and
actions.
You have to create in a memory shared space multiprocesing manager the following basic structures:
• list ipt :: list that manage process assignment
• list ico :: list that manage tasks on the active process
• dicctionary assigned_tasks :: dict that collect assigned tasks
• nproc :: int value with amount of assigned process
• dictionary sta_init :: dict with the flags to control task executions.
The following step is the assignment of your loop function in which you run your desired tasks. There are two cases:
• Unidirectional :: create a dictionary with your run_task async function. That function have to initialice the
classes for every task and then do the unidirectional work.
• Bidirectional :: create a dictionary with you have to separate the different actions. The ‘run_task’ is for initialize
the loop with the actions. The key net2service is the async function that come from network and send to service,
the key service2net is the async function that come from service and send to network,
On the main script you have to put the Scheduler object on the Assignator object, in that way every time you active a
task the assignator produce a message to activate the task on the Scheduler.
The magic on this clasess are the initial settings, in which you define the size of your machine:
• amount of processes
• amount of tasks by process
With that data the machine put in idle the main process task and by every process also in idle the amount of tasks.
When you start to assign the task to do something, the TaskAssignator object start to wake up the idle tasks. So on, is
not simple but works.
13
Async Task Tools Documentation, Release 0.5
I can say this class is the digievolution of the taskloop functionalities. I need to use those tools to collect an amount
of data in real time from a network of stations. If i used only a process the rate to produce a new data is large than the
capacity to process all at the right time. Because of that is necesary the use of more simultaneous processes to work
with all the assigned tasks.
In relation with the amount of stations is necesary to know
• amount of sources
• frequency of new data (f)
• capacity of number of stations or sources by processor in that frequency (cap(f))
We can define
• Amount of workers (procesors) to work with every source (n)
• Amount of tasks availables by every worker (m)
At the running time, the system is an empty machine with n workers in await status, and every worker with m tasks
also in await status.
When the metadata to connect every source arrive to the machine, maybe using an extra worker to attend socket
communication, the data is loaded and start to active the tasks, one source is one task.
And that is the idea. Big, happy, useful machines for everyone.
Here is useful to understand how works the queues and shared dicts or list on memory. Using the manager from
multiprocessing library you can activate this special objects.
The TaskAssignator class works with an instance of TaskScheduler’s subclass implemented specifically for your
goal.
This class handle the list of new data incoming and assign the task to some specifically worker. Register the action to
future control. Simple but effective.
Also you can control if you only need to active a group(for test purposes) or all the list of fata.
7 Args:
8 :param queue_tasks: a queue to put task ids
9
10 """
11 await asyncio.sleep(self.ts)
12 scheduler = self.scheduler
13 sta_assigned = self.sta_assigned
14 dt_status = self.dt_status
15 dt_group = self.dt_group
16 msg_in = []
17 try:
18 tasks = []
19 W=0
20 if not queue_tasks.empty():
21 for i in range(queue_tasks.qsize()):
22 ids = queue_tasks.get()
23 scheduler.status_tasks[ids] = True
24 scheduler.sta_init[ids] = True
(continues on next page)
15
Async Task Tools Documentation, Release 0.5
16 Chapter 5. The Walking Stick for The Old Scheduler: The Assignator
CHAPTER 6
Code’s Documentation
Those are the main functions to enable the async while. The core functions to create asynchronous code. Are like the
gears for a big future machine.
tasktools.taskloop.coromask(coro, args, fargs)
A coroutine that mask another coroutine callback with args, and a function callbacks who manage input/output
of corotine callback
Parameters
• coro – is a coroutine object defined by the developer
• args – the list of arguments to run on the corotine coro
• fargs – the function that process the input and create an output related with the coro result
Returns a result, is a list of the elements for future argument
tasktools.taskloop.renew(task, coro, fargs, *args)
A simple function who manages the scheduled task and set the renew of the task
Parameters
• task – is a Future initialized coroutine but not executed yet
• coro – is the corutine to renew when the first is finished
• fargs – the function to process input/output
• args – the unpacked list of extra arguments
tasktools.taskloop.renew_quamash(task, coro, fargs, loop, *args)
A simple function who manages the scheduled task and set the renew of the task. The same as renew but it can
be used with different event loop like quamash for qt
17
Async Task Tools Documentation, Release 0.5
Parameters
• task – is a Future initialized coroutine but not executed yet
• coro – is the corutine to renew when the first is finished
• fargs – the function to process input/output
• loop – a different event loop from asyncio
• args – the unpacked list of extra arguments
tasktools.taskloop.simple_fargs(_in, obtained)
Simple function who can be used in callback on coromask, the inputs are /_in/ and /obtained/ value from the
coroutine executed. Return _in
_in the input list
Parameters obtained – the object that came from the result of coroutine execution
Returns _in
tasktools.taskloop.simple_fargs_out(_in, obtained)
Simple function who can be used in callback on coromask, the inputs are /_in/ and /obtained/ value from the
coroutine executed. Return obtained
Parameters
• _in – the input list
• obtained – the object that came from the result of coroutine execution
Returns obtained
The Scheduler is a like abstract class the enables a set of features that you have to inherit to your own class.
class tasktools.scheduler.TaskScheduler(*args, **kwargs)
This class is a generic tasks scheduler that uses asyncio and multiprocessing For every new process activate M
idle coro tasks until when are assigned by a function that send an id
Parameters
• args – an unpacked list
• kwargs – an unpacked dictionary with at least {ipt dict, ico dict, assigned_task dict, nproc
int, sta_init dict}, all in shared memory manager
add_task(ids, ipt)
Add an ids task to some ipt process
Parameters
• ids – the key of a source
• ipt – the key or identifier of a process
manage_tasks(ipt)
A method to manage the tasks assigned to ipt process
Initialize an event loop, and assign idle tasks for this process
Create the tasks for every source assigned to this process. Check the cases unidirectional and bidirectional.
set_init_args()
Set the initial list of arguments
In your class you have to rewrite this.
Returns a list of initial arguments
set_ipt(uin=4)
Defines a new id for relation process-collect_task, check if exists
Parameters uin – is an optional int to define the length of the keys for ipt dict
Returns a new key from ipt
set_new_run_task(**coros_callback)
Is a function that you have to call to define the corutines which you need to execute on the wheel (gear)
Is recomended the use at the end of the __init__ method on your new class.
Parameters coros_callback – is a dictionary with the corutines, that depends of what you
need:
set_pst(ids, args)
Set the factory for the wheel’s array
In your class you have to rewrite this.
Parameters
• ids – key of the source
• args – list of arguments
Returns a different list of future arguments
This class works fine, you don’t have to implement a subclass of this, you have to use your Scheduler class on it.
class tasktools.assignator.TaskAssignator(scheduler, queue_tasks, sta_assigned, dt_status,
dt_group, *args, **kwargs)
Manage the tasks assigned to a TaskScheduler instance
Parameters
• scheduler – A TaskScheduler instance
• queue_tasks – a queue
• sta_assigned – a dict managed for multiprocessing
• dt_status – a string that select the kind of group {GROUP, ALL}
• dt_group – a list of the group selected
• args – some extra args
• kwargs – some extra keyword args
new_process(queue_tasks)
This coroutine activate a process with new station task Check every sseconds the queue_tasks if there are
new stations or tasksto add.
Works on a async wheel
Parameters queue_tasks – a queue to put task ids
new_process_task()
This function allows the system to call the coroutine that add a new_process in an asynchronous loop the
wheel
To do
What more
?
• Examples
23
Async Task Tools Documentation, Release 0.5
24 Chapter 7. To do
CHAPTER 8
• genindex
• modindex
• search
25
Async Task Tools Documentation, Release 0.5
A set_init_args() (tasktools.scheduler.TaskScheduler
add_task() (tasktools.scheduler.TaskScheduler method), method), 19
18 set_ipt() (tasktools.scheduler.TaskScheduler method), 20
set_new_run_task() (tasktools.scheduler.TaskScheduler
C method), 20
coromask() (in module tasktools.taskloop), 17 set_pst() (tasktools.scheduler.TaskScheduler method), 20
simple_fargs() (in module tasktools.taskloop), 18
M simple_fargs_out() (in module tasktools.taskloop), 18
manage_tasks() (tasktools.scheduler.TaskScheduler
method), 18
T
TaskAssignator (class in tasktools.assignator), 20
N TaskScheduler (class in tasktools.scheduler), 18
new_process() (tasktools.assignator.TaskAssignator
method), 20
new_process_task() (tasktools.assignator.TaskAssignator
method), 21
P
process_sta_manager() (task-
tools.scheduler.TaskScheduler method),
19
process_sta_task() (tasktools.scheduler.TaskScheduler
method), 19
process_sta_task_n2s() (task-
tools.scheduler.TaskScheduler method),
19
process_sta_task_s2n() (task-
tools.scheduler.TaskScheduler method),
19
R
renew() (in module tasktools.taskloop), 17
renew_quamash() (in module tasktools.taskloop), 17
run_task() (tasktools.scheduler.TaskScheduler method),
19
S
set_ico() (tasktools.scheduler.TaskScheduler method), 19
27