Reputation: 5118
I was just experimenting with some example from official docs and noticed that locking doesn't seem to work as advertised. What is happening?
from multiprocessing import Process, Lock
def f(l, i):
l.acquire()
for j in range(10000):
print j,
print
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(10):
Process(target=f, args=(lock, num)).start()
Ran in Ipython notebook, output as follows:
2716 2717 27180 1 2 3 4 5 6 7 8 9 10
Upvotes: 1
Views: 130
Reputation: 11315
It's simple. Do you really think you can spawn processes from within a web application? Cause that's what IPython Notebook actually is.
No. You can't. That would be a huge security hole if webapps were able to spawn processes at will. They can't do it on the client side cause it's all happening in a browser's sandbox. They shouldn't do it on the server side, cause you might want to spawn 1000000 processes this way and what? The server should kill itself?
Apparently multiprocessing
module is mocked in IPython Notebook and everything actually happens in one process - and this process owns the lock so everything is fine.
The output from normal Python and IPython interpreters is ok and it respects the lock if you don't try the funny idea of controlling processes via webapp.
Upvotes: 3