Hacker News new | past | comments | ask | show | jobs | submit login

Nice. I still haven't given Go much time, but I really like this form, where you can switch on multiple blocking calls.

I've definitely wanted this in other languages where I'm using thread safe queues for communication, and had to do manual multiplexing into a new queue whenever I wanted to do a blocking get on multiple queues.

Does anyone know of a convenient way to do this when handling multiple instances of python's Queue or similar with Haskell's Chan?





Thanks for posting this. I found something similar but on first blush didn't think it was quite right as I was mistakenly thinking that you could only do this for TChans with the same type. Looks like it might be just what I'm after! When I think about it, STM seems like it makes sense here, since you probably want the ability to cancel/rollback the blocking gets that didn't return.

EDIT: Actually, this isn't right. The example linked does "if get from channel 1 fails, get from channel 2". I'm looking for "select" on multiple channels, where the result is based on the first channel to return.

EDIT again: Apparently I can't read, and this does achieve what I'm looking for, I just have no basic experience with STM. Awesome, will try this later!


I've been implementing go-style channels in Python. Check it out here: https://github.com/stuglaser/pychan

Blocking on multiple channels works (chanselect).


njs12345 answered for Haskell, I don't remember seeing a way to do this in Python though depending on the situation you could just use a single queue for everything of course.

FWIW there's no primitive letting you wait on multiple conditions in threading, and Queue is implemented in terms of a pair of conditions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: