Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature enhancement: improvements to syntax around strands/concurrency. #25969

Open
ramith opened this issue Sep 21, 2020 · 3 comments
Open

Feature enhancement: improvements to syntax around strands/concurrency. #25969

ramith opened this issue Sep 21, 2020 · 3 comments
Assignees
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement

Comments

@ramith
Copy link
Contributor

ramith commented Sep 21, 2020

Description:
Please check following code fragment. some if the variants doesn't compile. Could you check if these makes sense?

function workload(int i) {
    io:println("running workload :" + i.toString());
}

function runit() {

    var result1k = wait start workload(1000);
    var result2k = wait {start workload(1000), start workload(1000)};//won't compile

    future<()> nextScene = start workload(3000);
    future<()> nextScene2 = start workload(3000);
    var result6k = wait {nextScene, nextScene2};// this compiles as shown in examples.
    

    int i = 0;
    future<()>[] responses = [];
    while (i < (<int>math:randomInRange(5, 10))) {
        responses[i] = @strand {thread:"any"} start workload(i);
        i = i + 1;
    }

    var result = wait responses; // wont compile
}

Steps to reproduce:
N/A
Affected Versions:
1.2.8

@manuranga

@ramith
Copy link
Contributor Author

ramith commented Sep 21, 2020

Following doesn't work too:

function run2() {
    record {|
        future<()>...;

    |} responses = {};

    int i = 0;
    while (i < (<int>math:randomInRange(5, 10))) {
        responses[i.toString()] = @strand {thread:"any"} start workload(i);
        i = i + 1;
    }


    var waitForAll = wait responses;// compile error here.

} ```

@ramith
Copy link
Contributor Author

ramith commented Sep 21, 2020

I had a chat with @manuranga and we concluded that it would be really useful to have the ability to wait for a dynamically created set of futures, stored either in a record or in a array.
@MaryamZi thanks for helping me figure out some parts of the code fragment.

@anupama-pathirage anupama-pathirage added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement labels Oct 29, 2020
@rdhananjaya
Copy link
Member

We can create more futures to solve this issue like below.

import ballerina/lang.runtime;
import ballerina/io;
import ballerina/random;

function waitHelper(future<any>[] fs) returns any|error {
    match fs {
        var [f] => {
            return wait f;
        }
        var [f, x] => {
            return wait f | x;
        }
        var [f, x, y] => {
            return wait f | x | y;
        }
        var [f, x, y, ...r] => {
            var rest = start waitHelper(r);
            return wait f | x | y | rest;
        }
    }
}
function foo() returns int {
    int r = checkpanic random:createIntInRange(10, 200);
    io:println("waiting (ms)... ", r);
    runtime:sleep((<decimal>r)/1000);
    return r;
}
public function main() {
    future<int>[] f = [];
    foreach int i in 0...9 {
        var r = start foo();
        f.push(r);
    }
    any|error got = waitHelper(f);
    io:println("Got: ", got);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement
Projects
None yet
Development

No branches or pull requests

4 participants