Calling function within class.

Vino akashvino79 at gmail.com
Thu Nov 19 19:51:24 UTC 2020


On Wednesday, 18 November 2020 at 21:33:58 UTC, Ali Çehreli wrote:
> On 11/18/20 7:01 AM, Vino wrote:
>
> >    Request your help on how to call a function(listFile) from
> another
> > function(getFilelist) within the same class(GetDirlist),
> below is an
> > example code.
>
> That code looks unnecessarily complex to me. First of all, 
> parallel() already executes the loop body in separate threads, 
> so I don't see any reason for Task in that code.
>
> std.parallel will appear during my DConf presentation on 
> Saturday. The following program applies ideas from some of my 
> slides and just works in parallel:
>
> import std.process;
> import std.exception;
> import std.format;
> import std.parallelism;
> import std.stdio;
>
> class GetDirlist {
>
>   @system private auto listFile(immutable string st)
>   {
>     auto fl = execute(["ls","-l"]);
>     enforce(fl.status == 0, format("File not Found: %s", 
> fl.status));
>     return fl.output;
>   }
>
>   @system public auto getFilelist()
>   {
>     // I am using D dynamic arrays for simpliticy
>     auto flstore = [ "/path1/Dir1", "path2/Dir2" ];
>
>     // Note preallocated slot for each result:
>     auto amidata = new string[flstore.length];
>
>     // Taking advantage of automatic loop counter
>     foreach(i, st; parallel(flstore,1)) {
>       // Each execution assigns to its own slot
>       amidata[i] = listFile(st);
>     }
>     return amidata[];
>   }
> }
>
> void main() {
>   // Need an object to call a non-static member function:
>   auto g = new GetDirlist();
>   writeln(g.getFilelist());
> }
>
> Ali

Hi Ali,

Thank you very much, your solution works for my example, but it 
does not work for the main goal, let me explain what we are 
trying to perform.

Nut shell: Try to execute an aws command on sever accounts in 
parallel to get some data.

Noe: each account has as separate username and password store in 
a database table(encrypted).

Cod Logic
Fetch the username/ password from the table for each account.

Get the “awssecrete” key and “accesskey” for each account by 
calling an aws api using the above username/password.

Set the fetched key’s as an environment variable.

Execute the aws command and get the data for each of the account

   As we have many accounts what we are trying is to get the data 
in parallel(execute the aws command in parallel for each account 
and store the result in a array). At present our code is working 
fine(without parallel), the moment we enable parallelism, it is 
throwing an error on the SQL part (Fetch the username/ password 
from the table for each account), as it could not execute the SQL 
query in parallel for different account. If there is any other 
logic please do let me know will give it a try. Below is the SQL 
code.

@trusted public auto getAwsconf(immutable string account)
{
  auto con = new GetConnections();
  Statement stmt = con.db.prepare("SELECT 
username,AES_DECRYPT(b.userpass,b.key,b.vector) AS passwd FROM 
config WHERE account = :account");
  stmt.setParameter("account", account);
  RowSet awsaccount = stmt.query();
  scope(exit) con.db.close();
  return awsaccount;
}

From,
Vino.B


More information about the Digitalmars-d-learn mailing list