Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active January 11, 2020 11:07
Show Gist options
  • Save osa1/98897ffafebf0e6d209edb2853c341af to your computer and use it in GitHub Desktop.
Save osa1/98897ffafebf0e6d209edb2853c341af to your computer and use it in GitHub Desktop.

Spawning async tasks in GTK apps

I'm trying to spawn async tasks in a GTK app using GLib's MainContext::spawn. Questions:

  • I'm not sure how to get MainContext of a GTK Application. I think Application should already have a MainContext, and I'm not sure if creating a new one is safe.

  • In a task spawned by MainContext::spawn I'm not sure which async I/O function/traits I can use. I think I can use AsyncRead/AsyncWrite from futures but that's not documented.

  • In a task I'm not sure how to spawn more tasks. Should I pass a copy of MainContext to my tasks and use it to spawn more tasks via the spawn method?

Any pointer on these would be appreciated.

@osa1
Copy link
Author

osa1 commented Jan 11, 2020

For (1) use MainContext::default()

@osa1
Copy link
Author

osa1 commented Jan 11, 2020

For (2) use stuff from "futures" or "gio" libs

@osa1
Copy link
Author

osa1 commented Jan 11, 2020

"the futures crate is completely runtime-independent"

@osa1
Copy link
Author

osa1 commented Jan 11, 2020

For (3) just do MainContext::default().spawn(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment