heal-research / SimSharp

Sim# is a .NET port of SimPy, process-based discrete event simulation framework
MIT License
126 stars 30 forks source link

Updated projects #34

Closed mit200 closed 3 years ago

mit200 commented 3 years ago

I want to add a async possibility to run SimSharp with Processes that return IAsyncEnumerable

mit200 commented 3 years ago

Sorry about that -> I'm not yet finished working on my Async implementation

abeham commented 3 years ago

Maybe you can share some thoughts on your use case and how you would be implementing this?

mit200 commented 3 years ago

Hi,

so I was wondering if I could add an async process to the Simulation (possible with IAsyncEnumerable/IAsyncEnumerator). Here is an example of which I want to do: Basically call an async Method on a Process image

I'm not a 100% sure if that would be a great feature for everyone since there is only support for .net Core 3.0 and higher or .net Standard 2.1. That's why I had to change the target framework.

Checkout my forked branch: https://github.com/mit200/SimSharp/tree/dev/tn/IAsyncEnumerable With the addition of the interface IEventAction I'm able to either add an Func<Event, Task> or the current Action<Event> to the Event.CallbackList for async processing. So basically everything is running async and I added the 'ProcessAsync.cs' for my purpose.

Intertested in what you think about it :)

abeham commented 3 years ago

Hmm, I have unfortunately little to no experience with async enumerable. But I would assume that the example you posted does not require an async enumerable. Couldn't you just block the call to OnUpdate with ".Result"? You don't yield return whatever the outcome is with simulation.OnUpdate so the event yielding is not asynchronous. Also you can't really advance the simulation clock until all those awaits are finished. Thus the only use case is to have another event being processed at the same simulation time. Is that what you want?

Does that foreach loop really run in parallel with all _simulations, I think you await the first, then move to the next and so on? You can also create a lot of tasks and wait upon the tasks being finished.

What would you want to achieve with that code?

mit200 commented 3 years ago

You are right also with the list of tasks! Right now it does not yet but I'll be working on that the next couple weeks. In the world of async programming I don't want to block the thread while waiting for work to be done behind. I'll keep you posted!

Thanks for your input.