This queue really just needs to be a FIFO, where I can push items to the end of it, and pop items from the beginning of it. And it could get quite large, so I can't store it in-memory, because then I'll likely run afoul of the OOM killer. The real catch here is that each instance of the script will need its *OWN* shared queue, available only to its own child processes, because I'll likely end up running multiple instances of it at once.
I had thought of making a db connection in the parent process before forking, then cloning the db handle for each child and using a mysql temp table for the "queue", but it seems cloning actually makes a brand new user session with mysql, so one child can't use (or even see) another child's temp tables (as mysql by design restricts access of a temp table to the session in which it was created). I can't easily use normal (permanent) tables because multiple instances of the script will step all over each other.
So, how could I cleanly and efficiently share what could potentially be a LARGE amount of data between multiple child processes, yet keep it confined to only that instance of the script?
Update:
I ended up using chrestomanci's solution, with just one table and using the pid of the parent as a key for each instance. This seemed like the best of both worlds (easy to implement, easy to understand), and since it's a mysql instance on the same server, it's producing some good throughput.
I might look into SQLite as salva suggested, though I'm using a named lock for popping from the queue (since I have to do a select, followed immediately by a delete) and I'm foggy on how (or if) i can exclusively lock a sqlite db in the same fashion.
Update 2:
It looks like BEGIN EXCLUSIVE TRANSACTION should do essentially the same thing as I was using a named lock for, explicitly preventing other processes from reading/writing until the transaction is ended.
In reply to A per-instance shared queue among forked child processes? by EvanK
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |