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

Priority of async tasks #2

Open
l4rzy opened this issue Dec 12, 2019 · 4 comments
Open

Priority of async tasks #2

l4rzy opened this issue Dec 12, 2019 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@l4rzy
Copy link
Owner

l4rzy commented Dec 12, 2019

Priority of async tasks is required to make the GUI fast.
For example, when Home is loading, clicking on a Book to view to see the book's cover and preview pages must have higher priority than async tasks that are loading images in Home.

@l4rzy l4rzy added enhancement New feature or request help wanted Extra attention is needed labels Dec 12, 2019
@nomi-san
Copy link

nomi-san commented Jan 4, 2020

Have you tried multi-threading?
In many platforms, GUI cannot process on multiple threads.
But the image data can be loading in background on another thread.

e.g

/* thread 2 */
void imgLoader(...) {
    data = net.get(...);
}

/* callback */
void onInitialized() {
    thread.run(imgLoader, args...);
}

/* callback */
void onChangeScene() {
    if (scene.type == SC_BOOK) {    // switch
        await(imgResult);
        ...
    }
}

/* main thread */
int main() {
    prepare();
    app.run(win);
}

@l4rzy
Copy link
Owner Author

l4rzy commented Jan 4, 2020

Thanks for replying.

From the very first day developing this application, I've used asynchronous task to load images. Interestingly, I've found that asynchronous functions blocked the UI just a little bit, especially when the internet is slow, as pointed out at this. And yes, I've successfully implemented network requests in another thread and everything worked alright.

But that was a different problem.

This issue is more like, how to pause an async task, and resume it later. The very common use case of this is when you have many async tasks, but some tasks have higher priority, e.g. your homepage has 30 images, but you want to load images that are in the perceptible field first, as soon as the user scroll to that point.

@nomi-san
Copy link

nomi-san commented Jan 5, 2020

I think, all images in the page should be loaded after the user accesses the page.
Unloaded images can be placed by something alternative (alt image/text). After loaded, image loader triggers event to GUI and shows.

void imgLoader(...) {
    data[imgIndex] = net.get();
    setStateOrYield(loadingState, ...);
}

void onScroll(...) {
    img = getImage(idx);
    if (img) {
        img.show(position);
    }
    else {
        delayOrWaitOrShowAlternativeImg(...);
    }
}

@l4rzy
Copy link
Owner Author

l4rzy commented Jan 6, 2020

I did some research on pausing and resuming async tasks in Vala, still no luck though.

I think your approach was clear and feasible to implement. By delaying loading and instead of loading and pausing, this would be more efficient.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants