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

Coroutines support #47

Closed
9 tasks done
czyzby opened this issue Apr 15, 2017 · 14 comments
Closed
9 tasks done

Coroutines support #47

czyzby opened this issue Apr 15, 2017 · 14 comments
Assignees
Labels
async Issues of the ktx-async module
Milestone

Comments

@czyzby
Copy link
Member

czyzby commented Apr 15, 2017

  • Add ktx-async module.
  • Implement coroutine context for LibGDX applications that executes tasks on the main rendering thread, using Gdx.app.postRunnable to resume coroutines executed on other threads.
  • Implement utility functions for asynchronous tasks based on coroutines executed on a configurable AsyncExecutor instance.
  • Add HttpRequest utilities based on coroutines.
  • Simplify LibGDX Timer API. Currently it does not support lambdas.
  • Add suspending delay method, which does not block the main rendering thread using LibGDX Timer.
  • skipFrame suspending method that resumes on the next frame using Gdx.app.postRunnable API.
  • Implement tasks cancellation.
  • Documentation with usage examples.

Implement ktx-async module with asynchronous operations utilities and Kotlin 1.1 coroutines support.

@czyzby czyzby added the async Issues of the ktx-async module label Apr 15, 2017
@czyzby czyzby added this to the 1.9.6 milestone Apr 15, 2017
@czyzby czyzby self-assigned this Apr 15, 2017
@czyzby czyzby mentioned this issue Apr 15, 2017
2 tasks
@czyzby
Copy link
Member Author

czyzby commented Apr 16, 2017

@kotcrab Sweet. LibGDX HttpResponse implementation is not thread-safe and does not care if the underlying connection was closed, silently ignoring all exceptions. Here I was, wondering why my implementation of asynchronous HTTP requests always returns empty content - I debugged LibGDX code and now I'm surprised it even works in the first place.

@czyzby
Copy link
Member Author

czyzby commented Apr 18, 2017

@sreich @kotcrab How should I call the most commonly used method that gives you access to KTX coroutines context and allows to execute code on the main rendering thread? onMainThread, execute?

czyzby added a commit that referenced this issue Apr 18, 2017
@kotcrab
Copy link
Contributor

kotcrab commented Apr 18, 2017

No idea. execute is too vague in my opinion. onMainThread or onGdxThread is ok-ish. I probably like most postRunnable, same as Gdx.app.postRunnable. Maybe just post but that seems vague if it's a global method.

@czyzby
Copy link
Member Author

czyzby commented Apr 18, 2017

postRunnable might be misleading, as coroutine can be executed on the same thread right after you start it, and it can create more than a single task objects under the hood. onMainThread isn't ideal either, as your coroutine might never do anything significant on the main thread. Maybe something more explicit like gdxCoroutine or ktxCoroutine?

The official method for creating coroutines with a context is launch. Our method would basically be a convenience wrapper over launch(KtxAsync). Any particular names with launch sound good?

@kotcrab
Copy link
Contributor

kotcrab commented Apr 18, 2017

I see. Nothing really strikes my mind though. I guess something like ktxAsync is not ideal either?

@czyzby czyzby mentioned this issue Apr 18, 2017
3 tasks
@czyzby
Copy link
Member Author

czyzby commented Apr 19, 2017

Not ideal, but simple and obvious. Not only does it match the module name, but also does not suggest where the code is executed (onMainThread) - only that it can be executed asynchronously. It's so trivial to add such method or simply use KtxAsync context directly with launch, that I don't think ktxAsync method will be harmful.

@yhslai
Copy link
Contributor

yhslai commented Apr 20, 2017

I personally +1 for onMainThread because when one use this kind of behavior he has to be aware of which thread the tasks will be executed.

Or onNextFrame, maybe? "Frame" sort of implies the thread that can render frames, a.k.a. main thread. Just my 2 cents.

@czyzby
Copy link
Member Author

czyzby commented Apr 20, 2017

But that's the thing: it does not have to be executed on the main thread. If you're using coroutines in the first place, chances are half of the instructions will be executed elsewhere. Only the processing of async operations results and non-suspending methods are invoked on the rendering thread. It might give you a false impressions that time consuming methods will block your rendering.

onMainThread {
  val httpResponse = httpRequest(url = "example.com", method = "GET") // Net executor.
  delay(1) // Timer.
  val result = asynchronous { someTask() } // Context's AsyncExecutor. 
}

@sreich
Copy link
Contributor

sreich commented Apr 20, 2017

Only the processing of async operations results and non-suspending methods are invoked on the rendering thread

I don't think you meant to say "non suspending methods". Suspending methods can be on any thread including the render thread and it can be useful to use them on the rendering thread to mask state.

Or am I misunderstanding here?

@czyzby
Copy link
Member Author

czyzby commented Apr 20, 2017

That's right, I oversimplified. I should have said non-suspending methods and suspending methods that can be executed on the main thread. KtxAsync context always tries to execute everything on the main rendering thread, besides the obvious cases where the actual work is delegated to other threads.

@czyzby
Copy link
Member Author

czyzby commented Apr 21, 2017

@kotcrab @sreich @raincole @MrPlow442 Can you take a look at the new module implementation and API? #57 I want your approval before merging and releasing this.

czyzby added a commit that referenced this issue Apr 21, 2017
@yhslai
Copy link
Contributor

yhslai commented Apr 21, 2017

I'm heading for ludum dare this weekend. If no one else does I can help review it next week.

@czyzby
Copy link
Member Author

czyzby commented Apr 21, 2017

It's OK. Good luck. ; )

czyzby added a commit that referenced this issue Apr 22, 2017
New module: ktx-async implementation. #47
@czyzby
Copy link
Member Author

czyzby commented Apr 22, 2017

@kotcrab Thanks for the #57 code review. There's already a lot of changes since the last version, so I'm going to release 1.9.6-b2 now and move KTX to the new repository. Coroutines-based asset loader is postponed to the next release.

@czyzby czyzby closed this as completed Apr 22, 2017
czyzby added a commit that referenced this issue Apr 22, 2017
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async Issues of the ktx-async module
Projects
None yet
Development

No branches or pull requests

4 participants