Python threading SUCKS, Multiprocesses dont (as much)
Posted: Tue Feb 03, 2015 10:09 pm
This is my story of learning the hard way that python threading blows, and that its (almost) completely useless..
About 5 months ago I was hired into a crypto exchange, a fairly large one. ( cryptsy.com ) And we got to work on a new cloud mining thing that was real. ( mintsy.co ) ( Majority of 'cloud mining' are ponzi schemes.. )
So I am the dev for the proxy which is what controls all miners connected to us, and matches them up to which pool they should mine at and where. Seems fairly trivial and I got the first one running in a day no problem since python's threading was super awesome and sharing memory was no problem at all. Last week we started stress testing (after months of my proxy running with little load on it) and it got fucking demolished..
Messages coming from the pool were minutes delayed sending to the miner, and I had no clue why. After doing more research on pythons threading and how it works, I found this thing called the GIL. Or 'Global Interpreter Lock'. Basically, pythons threading runs one interpreter and thus only interprets one thing at a time, and threads are NOT parallel. So I would have been better off in a single thread on a single super power core rather than on a dual cpu multi core server. Infact, more cpus and more cores MAKES PYTHON MULTITHREADING EVEN SLOWER. It makes no sense whatsoever..
After a code-a-thon over this last weekend, I rewrote the entire proxy using python's multiprocess. Which really is a duplicate of pythons threading, except now you cant share memory (obviously, its in a different memory space..) BUT python has these awesome things called queues which you can use to pass information across different processes. Anyways, in the end I ended up launching every miner and every pool connection as a separate process. And suddenly the dev box looked like this http://puu.sh/fohMv/8212aec90b.png
My drunk rant about python threading which caused much headache over a couple days of not knowing what the fuck was going on, and being forced to not sleep and rewrite everything from scratch.
About 5 months ago I was hired into a crypto exchange, a fairly large one. ( cryptsy.com ) And we got to work on a new cloud mining thing that was real. ( mintsy.co ) ( Majority of 'cloud mining' are ponzi schemes.. )
So I am the dev for the proxy which is what controls all miners connected to us, and matches them up to which pool they should mine at and where. Seems fairly trivial and I got the first one running in a day no problem since python's threading was super awesome and sharing memory was no problem at all. Last week we started stress testing (after months of my proxy running with little load on it) and it got fucking demolished..
Messages coming from the pool were minutes delayed sending to the miner, and I had no clue why. After doing more research on pythons threading and how it works, I found this thing called the GIL. Or 'Global Interpreter Lock'. Basically, pythons threading runs one interpreter and thus only interprets one thing at a time, and threads are NOT parallel. So I would have been better off in a single thread on a single super power core rather than on a dual cpu multi core server. Infact, more cpus and more cores MAKES PYTHON MULTITHREADING EVEN SLOWER. It makes no sense whatsoever..
After a code-a-thon over this last weekend, I rewrote the entire proxy using python's multiprocess. Which really is a duplicate of pythons threading, except now you cant share memory (obviously, its in a different memory space..) BUT python has these awesome things called queues which you can use to pass information across different processes. Anyways, in the end I ended up launching every miner and every pool connection as a separate process. And suddenly the dev box looked like this http://puu.sh/fohMv/8212aec90b.png
My drunk rant about python threading which caused much headache over a couple days of not knowing what the fuck was going on, and being forced to not sleep and rewrite everything from scratch.