eneural-net / async_task

Asynchronous tasks and parallel executors, supporting all Dart platforms (JS/Web, Flutter, VM/Native) through transparent internal implementations with `dart:isolate` or only `dart:async`, easily bringing concepts similar to Thread Pools to Dart, without having to deal with `Isolate` and port/channel complexity.
Apache License 2.0
53 stars 4 forks source link

Want to use the logger instance set up in the main thread #13

Closed myConsciousness closed 2 years ago

myConsciousness commented 2 years ago

Hi amazing developer!

I'm trying to get this library working for @batch-dart and have a question about logging.

The framework I'm developing currently generates the logger as a singleton instance in the main thread, but this singleton instance will not work once parallel processing starts in this library.

I think the reason this will not work is because it will be processed in a separate thread, but I would like to somehow use the singleton instance created in this main thread without recreating it.

From what I've seen, I think I can use the SharedData specification, but do you have any other suggestions?

Thank you!

gmpassos commented 2 years ago

I understood your problem, but Dart Isolates actually won't allow instance sharing.

The Isolate parallelism model fully separates all objects between Isolates. It doesn't work like classical threads, where all the memory is shared and you need to control the access to shared memory.

Async Task tries to simplify the abstraction of Isolate to a task paradigm. The shared data of a task is just the shared data between tasks of the same "group", avoiding the need to resend the same data for multiple tasks.

One question. Why do you need a single instance for logging?

myConsciousness commented 2 years ago

@gmpassos

Okay I understand!

Although somewhat aggressive, I solved this problem by creating a logger instance for parallel processing before parallel processing starts only when using this library. This is a somewhat exceptional for the framework's philosophy, but I will test it with this for the time being :)

Perhaps in the future, I may cut out some of the processing of library and use it so that it can be integrated into the framework's philosophy, and maybe I call you again at that time!

The reason for making the logger a singleton is to allow logging from anywhere during the framework's lifecycle without creating an instance. This is a concept similar to Spring and Lombok in Java :)

Thank you!

gmpassos commented 2 years ago

Unless you need to keep a singleton file/socket open, you don't need a single logging instance.

Do not forget that to send some text/data to another isolate it will be serialized. It's not simple like call a method in a normal Java logging (like log4j).

The best way to think about this paradigm is that an Isolate is like a separated VM in the same process. Dart does a good job reducing the cost of each Isolate and to reduce the memory needed for each one, but each Isolate actually behaves like a separated "processes".

One approach for centralized logging is to have the main processor of logging "messages" in a separated Isolate and send all the messages to it using an AsyncTaskChannel. Take a look at:

https://pub.dev/packages/async_task#asynctaskchannel

myConsciousness commented 2 years ago

Thanks for your support @gmpassos !

I checked the AsyncTaskChannel specification. It is true that this feature would allow messages to be sent and received between the main thread and parallel threads :)

I will have to think a bit more about the specification design of my framework in order to integrate this feature successfully, but I will use this library anyway!

Thank for your kindness!