Où sommes nous ?

Retour

Shared variables

Séléné is multi-thread aware, especially because it has to handle asynchronous MQTT messages arrival. But Lua itself is not multi-thread safe and consequently, Lua context (in other words, variables in your Lua code) can't be exchanged between threads.

SelShared is a special kind of "variables" managed by Séléné for data exchange between threads. They can be only text or numeric ones (you can't store function, arrays or other types of data Lua is knowing).

One common mistake while working with thread is variables' scope : if a function is called outside the main thread (i.e MQTT callback), all global variable are not reachable and raise "calculation with nil value" error.

variables related methods

SelShared.set( name, value, ttl )

This function set a variable's value, creating the variable if needed. Arguments are

Example :

SelShared.set("myvar", "coucou")

"myvar" contains the string coucou without expiration.

SelShared.set("ttl", 2, 5)

"ttl" contains the number 2 which remain valid only for 5 seconds.

SelShared.set("var", {})

the variable "var" contains invalid content : tables are not part of allowed type to be stored

SelShared.get( name )

returns the variable's content. Nil if invalid or undefined.

SelShared.mtime( name )

Returns the last moditfication time for a variable (timestamp)

SelShared.dump()

Displays all variables with their contents, expiration time and linking information. Lists also waiting tasks.

This function is for debugging purposes but, as it should be useful during normal development, I keep it available even without DEBUG defined at compilation time.

Testing

For a test of SelShared variable manipulation, have a look on this script which is also provided with Séléné sources.

It produces such output :

Initial situation
var's value :   coucou
ttl's value :   2
*D* Dumping variables list f:0x1dde010 l:0x1dde068
*I* name:'var' (h: 329) - 0x1dde010 prev:(nil) next:0x1dde068 mtime:Fri May  5 16:54:55 2017
        DString : 'coucou'
*I* name:'ttl' (h: 340) - 0x1dde068 prev:0x1dde010 next:(nil) mtime:Fri May  5 16:54:55 2017
*I*     5.000000 second(s) to live
        Number : 2.000000
*D* Dumping pending tasks list : 0 / 0


Testing variable behaviours
-----------------------------

Setting "var" with an invalid type :
> SelShared.set() returns :     nil     Shared variable can be only a Number or a String
And its mtime is Fri May  5 16:54:55 2017

"ttl" variable has now expired

var's value :   nil
ttl's value :   nil
*D* Dumping variables list f:0x1dde010 l:0x1dde068
*I* name:'var' (h: 329) - 0x1dde010 prev:(nil) next:0x1dde068 mtime:Fri May  5 16:54:55 2017
        Unknown type or invalid variable
*I* name:'ttl' (h: 340) - 0x1dde068 prev:0x1dde010 next:(nil) mtime:Fri May  5 16:54:55 2017
*I*     This variable is dead
        Unknown type or invalid variable
*D* Dumping pending tasks list : 0 / 0

Visitez :
La liste de nos voyages
Nos sorties Ski et rando
Copyright Laurent Faillie 2001-2024
N'oubliez pas d'entrer le mot de passe pour voir aussi les photos perso.
Contactez moi si vous souhaitez réutiliser ces photos et pour les obtenir avec une plus grande résolution.
Visites durant les 7 derniers jours Nombre de visites au total.

Vous pouvez laissez un commentaire sur cette page.