6 ms·
Um, no. Your advice is completely backwards. Perl does NOT have a lightweight threading model. Period. Literally every time you spawn a thread, Perl copies
by btilly 17y ago
Um, no. Your advice is completely backwards.
Perl does NOT have a lightweight threading model. Period. Literally every time you spawn a thread, Perl copies all of its data structures to avoid sharing stuff that is not threadsafe. If your Perl developers try to tell you otherwise, they are incompetent. The standard way to build a high performance website in Perl is to use prefork and then put a reverse proxy in front of them in httpd accelerator mode. See http://perl.apache.org/docs/1.0/guide/strategy.html http://perl.apache.org/docs/1.0/guide/strategy.html for an old, but still accurate, description of httpd accelerator mode if you don't know what it is.
Please note that Windows does not have good support for forking. Therefore I have never heard of a competent team of Perl developers choosing to try to deploy a high volume website on Windows.
To the best of my knowledge both Ruby and Python do support lightweight threading, unlike Perl, but both have a global interpreter lock that synchronizes very frequently, and severely limits scalability. I don't know what strategies are common in those languages, but personally I suspect that the right strategy on Unix with those languages is, like Perl, to use pre-fork with a reverse proxy in httpd accelerator mode.
However both Python and Ruby have implementations on other platforms, such as the JVM. I believe (verify this before relying on it) that those implementations support lightweight threads without the drawbacks of the C implementation. Therefore if you want to use lightweight threading with that class of scripting language, use one of those implementations.
- jrockway 17y agoPerl's lightweight threading is provided by Coro.
- btilly 17y agoCoro is just a cooperative asynchronous framework on par with, say, POE. You do not get to use multiple CPUs. You do not get to use a database. You do not get to use a normal multi-threaded webserver. I would be suspicious of anyone seriously trying to create a high volume Perl website using Coro.
- jrockway 17y agoYou are right about multiple CPUs. For that, fork and load-balance. Instead of each forked process handling one connection, it handles a few thousand. If your app is CPU intensive, I have to wonder why you'd use Perl for that. You get a 2x speedup by using multiple cores, but a 50x speedup by switching to Haskell or Common Lisp for the critical section. (You could also use C or C++ or Java, but that's just being crazy.) As for databases, most real databases have non-blocking interfaces; this means database queries won't stall your threads. (Postgres and BDB are known to work well. MySQL requires hacks.) And yes, you don't get to use a normal multi-threaded webserver. I am not sure how that works. This all scales quite well.
- btilly 17y agoYes, you can use some databases. But I don't think you can use the classic DBI interface. More problematic, though, is that a single poorly coded function call on a seldom hit page can seriously impact responsiveness for a large fraction of your website. With real threads or processes you can protect against a function with a memory leak with resource limits. (You can set that up BSD::Resource with Perl on Unix systems.) If you try that with Coro you risk taking down a large fraction of your website each time a bad function runs. If you have a small team and absolutely trust their work, this works. But as you scale up in complexity, mistakes will happen. You will have problems. And your choice of cooperative multitasking will look worse and worse. Cooperative multitasking is not a new idea. In fact it is usually the first thing people try. Windows through 3.1 (continued under the hood in the 9x line). Apple did it through OS 9. Ruby did it through the 2.8 line. Yet in every case people run into the same problems over and over again and conclude that they are better off with preemptive multitasking. Even badly done preemptive multitasking such as Windows 95 or Ruby 2.9. Again. I would be suspicious of anyone trying to create a high volume website these days in Perl relying heavily on cooperative multitasking. I'm not going to say that they won't succeed. But they are setting themselves up for problems down the road. Furthermore it isn't as if there is a real problem that needs solving here. A single decent server properly set up can serve enough dynamic content from a single machine to put you in the top few thousand sites. Buy more machines and you can scale as far as you want.
- nothingmuch 17y agoPerl has cooperative threads mapped to a single posix thread with Coro. Python and Ruby both have preemptive threads on top of posix threads. A mutex ensures only one posix thread is actually running at any given point in time. Ruby versions before 1.9 also used a single posix thread, requiring memcpy to swap the stack. Coro suffers from a similar problem (the contents of the PADLISTs of every sub found on the context stack has to be swapped in and back out). Preemptivity for Coro can be implemented very similarly to Ruby and Python (e.g. using Async::Interrupt). The "lightweight" that unifies all implementations is that only a single thread runs at any given point in time, even when SMP is available. This means that even though it's not a shared nothing model (like ithreads), there's no need to take a mutex or spinlock for every variable access.