-
Notifications
You must be signed in to change notification settings - Fork 521
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
Event loop implementation? #2333
Comments
It occurred to me, another place this could be very useful is in fs2's sync compiler, which gets tripped up when used to run streams that need See: |
Not sure whether it could be the solution for this use case, but I'm posting it, as we discussed on discord: while trying to work with some Java frameworks, like QtJambi or LWJGL, sometimes it's required for certain functions to be run on the main thread. A workaround could be to execute what's needed to be run on the main thread before entering the I don't have a repo to show the use case, but a sample snippet could look like more or less like: package com.example
import cats.effect.{IO, IOApp}
import io.qt.widgets.*
object Sample extends IOApp.Simple:
override val run =
for
app <- IO.delay(QApplication.initialize(Array.empty))
_ <- // IO stuff running outside the main thread
_ <- IO.delay(QMessageBox.information(null, "QtJambi", "Hello World"))
_ <- // even more IO stuff running outside the main thread
_ <- IO.delay(QApplication.shutdown)
yield () |
Crazy idea, but what if you could do something like |
Yes, I was thinking exactly about that approach (best solution imho because of the fine granularity) when I faced the problem, but I didn't know if that could be achievable, or what the implications to the rest of the program would be even if that |
I mean, it's doable in a couple different ways. The really easy one is made pretty clear by @vasilmkd's changes in #2724. While the application is running, the main thread is just chilling out in a There are some details to work through though. Among other things, the syntax is likely to be worse, since So I'm tempted to say that this is something which could really only be done within an |
Makes sense, the main thread is something closely related to the application entry point, so I'd expect to only be able to access it from within an |
@bilki it's not just the main thread, with e.g. AWT (and by extension, swing) you sometimes need to run things synchronously in the event dispatch thread |
@ritschwumm actually it seems like it is already possible to run tasks on the AWT event dispatch thread without any changes in CE. I see there is a method So all you need is something like: object AwtEventDispatchEC extends ExecutionContext {
def execute(r: Runnable): Unit = SwingUtilities.doLater(r)
def reportFailure(t: Throwable): Unit = t.printStackTrace()
}
IO(whatever).evalOn(AwtEventDispatchEC) Since the main thread is special, it requires the changes proposed in #2732. |
|
I think we're pretty much set with this. Besides #2732, my original "why"s no longer seem important.
|
This has come up at least a couple times in different contexts and I think this would be a cool idea with some interesting applications. Plus @keynmol may have volunteered himself to take this on 😉
Why:
IO#unsafeRunSync
in single-threaded environments (e.g. AWS lambda) without the penalty of creating threads or shifting.unsafeRunSync
(!!) via a new "TemporalSyncIO
" data type that implementsTemporal
andSync
. @djspiewak pointed out this would be similar to the microtask loop (with the addition of timers).The text was updated successfully, but these errors were encountered: