4 ms·
What if I want to write a server such that each request is executed in a potentially different locale? You can use uselocale() to install a thread-specific loc
by caf 8y ago
What if I want to write a server such that each request is executed in a potentially different locale?
You can use uselocale() to install a thread-specific locale.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/uselocale.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/us...
- kazinator 8y agoThat's useful to know; so they finally patched this, some ten years ago now.
- haimez 8y agoThen your thread better only execute one request at a time and carry through every operation involved in that request. Works fine in PHP, not fine in most performant and even most non performant web contexts.
- kazinator 8y agoBut that's fine. For example, you pull the thread from a pool and bind it to the request. As part of this, the thread pulls the locale object from the request and attaches to it. It services that request, and then returns to the pool. Next time it is pulled from the pool it gets a request in a different locale. Other disciplines are possible. E.g. some operation involving multiple locales, all in one thread, can just do the uselocale calls as needed.
- int_19h 8y agoThat's not fine, because processing a single request often involves waiting on some I/O, and the thread can service (a part of) another request in the meantime. Which is what modern event loop driven servers do - but this doesn't play well with thread locals, or abstractions implemented on top of them, because now your single physical thread effectively runs multiple cooperatively scheduled logical threads/requests.
- ecnahc515 8y agohaimez said this already, but I wanted to phrase it another way: This only works if your not executing multiple requests in the same thread, ie: using non-blocking IO.
- caf 8y agoEven if you're using non-blocking or asynchronous IO there's always going to be a point at which you switch to the context of a particular request in order to handle it, and that's the point at which you can also switch locales if you want to handle it in a locale-specific way.