Re: photon v0.9.0 with Go-style D-flavored channels!
On Friday, 3 May 2024 at 17:12:40 UTC, Dmitry Olshansky wrote: On Monday, 29 April 2024 at 20:50:59 UTC, Dmitry Olshansky wrote: On Monday, 29 April 2024 at 20:50:24 UTC, Dmitry Olshansky wrote: Photon is a minimalistic multi-threaded fiber scheduler and event loop that works transparently with traditional blocking I/O C/C++/D/Rust libraries w/o degrading performance. And now we have Channels, gentelmen. The only missing bit is `select` function to multiplex on a bunch of channels. And the wait is over! Now there is a select function to multiplex on read side of a bunch of channels. This also fixes a bug in the poll syscall override with multiple events on posix systems https://github.com/DmitryOlshansky/photon/blob/master/examples/select.d ```d module examples.select; import std.range, std.datetime, std.stdio; import photon; void main() { startloop(); auto first = channel!(int)(2); auto second = channel!(string)(1); go({ delay(500.msecs); first.put(0); first.put(1); delay(500.msecs); second.put("ping"); }); go({ foreach ( _; 0..3) { select( first, { writefln("Got first %s", first.take(1)); }, second, { writefln("Got second %s", second.take(1)); } ); } }); runFibers(); } ``` --- Dmitry Olshansky CEO @ [Glow labs](https://glow-labs.pro) https://olshansky.me/about/
photon v0.9.0 with Go-style D-flavored channels!
On Monday, 29 April 2024 at 20:50:59 UTC, Dmitry Olshansky wrote: On Monday, 29 April 2024 at 20:50:24 UTC, Dmitry Olshansky wrote: Photon is a minimalistic multi-threaded fiber scheduler and event loop that works transparently with traditional blocking I/O C/C++/D/Rust libraries w/o degrading performance. And now we have Channels, gentelmen. The only missing bit is `select` function to multiplex on a bunch of channels. So here is example of Go-style, D-flavored channels: ```d import std.algorithm, std.datetime, std.range, std.stdio; import photon; void first(shared Channel!string work, shared Channel!int completion) { delay(2.msecs); work.put("first #1"); delay(2.msecs); work.put("first #2"); delay(2.msecs); work.put("first #3"); completion.put(1); } void second(shared Channel!string work, shared Channel!int completion) { delay(3.msecs); work.put("second #1"); delay(3.msecs); work.put("second #2"); completion.put(2); } void main() { startloop(); auto jobQueue = channel!string(2); auto finishQueue = channel!int(1); go({ first(jobQueue, finishQueue); }); go({ // producer # 2 second(jobQueue, finishQueue); }); go({ // consumer foreach (item; jobQueue) { delay(1.seconds); writeln(item); } }); go({ // closer auto completions = finishQueue.take(2).array; assert(completions.length == 2); jobQueue.close(); // all producers are done }); runFibers(); } ``` Obligatory link: https://github.com/DmitryOlshansky/photon --- Dmitry Olshansky CEO @ [Glow labs](https://glow-labs.pro) https://olshansky.me/about/