I’d like to define some global configuration parameters to share them across sessions. How do I declare and define global variables in DolphinDB?
you can define global variables using the global keyword.
global x = 1
Solution #1:
You can create a thread-safe dictionary which is shared across sessions. For example, create a global configuration file:
config = syncDict(STRING, STRING, `config)
config[`key1]=`value1
config[`key2]=`value2
Solution #2:
The first solution can only take effect on one node. To share configuration among nodes while maintaining the consistency, it is recommended to use a high-availability stream table.
colNames = `key`value`modifyTs
colTypes = [STRING, STRING, TIMESTAMP]
t=table(1000:0,colNames,colTypes)
haStreamTable(2, t,`global_config,10000000, keyColumn=`key, retentionMinutes=9999999)
In this case, you can even use DolphinDB as ZooKeeper. The configuration can be shared across the cluster, and can be restored after crash.
Related
In my application I have multiple variables that need to be accessed globally from the different functions of my script:
var a=1,b=2,c, ...;
Where "c" undefined at the beginning and takes value produced by some of the functions during scripts execution.
In order to declare them from within my "main" function I'm trying to use window object:
window.a=1;
window.b=2;
window.c;
This works, however I'm not sure if such approach is correct.
And is there is a way to avoid creation multiple window objects for each variable and combine them into more compact structure? Something like:
window.a=1,.b=2,.c;//---of course-this doesn't work
I was asking this question today too. It used to be the way when I last wrote javascript (mid-90s) and still seems to be the case see this resource
I just created one c program to create threads using POSIX thread library functions.I didn't use pthread_attr_init() function in that. Even my program works fine.So, what is the use of pthread_attr_init() and what does it do...? I am not familiar in thread concepts.Can anyone tell me is it compulsory to use pthread_attr_init() in thread concept program..?
pthread_attr_init is used to initialise a thread attributes structure, which can then be passed to pthread_create.
If you are creating threads with default attributes, you pass a NULL pointer for the thread attributes argument to pthread_init and there is no need to initialise an attribute structure.
However, if you want to configure specific thread attributes, such as scheduling policy, priority, concurrency level, then you must use pthread_attr_init to initialise the attribute structure before manipulating it using the attribute accessors functions (pthread_set... and pthread_get...) and passing it to the pthread_init function.
I need to export the keys and values from map in Dart. In PHP I use for that purpose function extract():
$array=array('one'=>1,'two'=>2,'three'=>3);
extract($array);
But I don't know, how to do the same thing in Dart. Is there any special function or construct for it? Or how can I reach the same result with forEach()? Is there anybody, who could help me?
Update 1: My target is from the map like this, but much complicated (example taken from Dart up and running):
var gifts = {
// Keys Values
'first' : 'partridge',
'second' : 'turtledoves',
'fifth' : 'golden rings'
};
in which I want to rewrite the result by any simple function or forEach() loop:
// something like gifts.forEach()? but how?
into variables:
assert(first=='partridge');
assert(second=='turtledoves');
assert(fifth=='golden rings');
//wow, rewritten! The code can continue and use just the variables:
querySelector('#animal').text=first;
Dart is a statically declared language. You cannot create new variable names at runtime, from a map or in any other way.
In order to refer to a variable, it must already be declared. That means that even if you could introduce new variables, you could not have any references to it in your existing code.
If the variables are already declared, and you just want to assign the values to them, you can use the mirror system, but I wouldn't recommend that. It is much simpler to just access the values directly in the map.
I don’t understand the difference between let mapleader="," and let g:mapleader=",". I know that g: means that it’s a global variable, but I don’t clearly understand the difference. Which one should I use in my .vimrc file?
If the aforementioned statements are both located outside of function
definitions, they have the identical effect of setting a global variable. However, if the first statement, without the g: prefix, is used in
a function body, it defines a variable local to that function.
See :help internal-variables and especially :helpg In a function:.
Hence, outside function definitions one can access the global map-leader
variable simply as mapleader.
let mapleader=","
as stated in the doc.
Why would you want to use g:mapleader? g: is mostly used in plugins to let global variables in order to expose settings to other plugins or for you to play with in your .vimrc.
I have this boolean variable that need to be accessed by two functions. In C programming, instead of declaring as global variable, is there any other way?
No there is not. This is what global variables are for.
You can send the value as a parameter if you only need your two functions to read the variable.
If you also need to change the variable from within the function, you will need to either use a global variable, or to send a pointer to the variable (which could be declared locally in whatever function ends up calling these other functions).