Re: [BangPypers] real use case of gevent context switch gevent.sleep(0)
Thanks noufal, I got it. After gevent.spawn, we have to do join or gevent.joinall or gevent.sleep or patched library call to start the greenlets, right ? I have see the code which not using any of these after gevent.spawn, How it is working there ? https://github.com/Juniper/contrail-controller/blob/master/src/opserver/uveserver.py#L656 Thanks, On Sun, Jun 21, 2015 at 1:01 PM, Noufal Ibrahim KV nou...@nibrahim.net.in wrote: On Sun, Jun 21 2015, anu sree wrote: [...] This code has two pause (gevent.sleep(0)), in Consumer.start and Worker.run. Here control goes to Worker.run when Consumer.start pauses and Consumer.start gets control back when Worker.run pauses. There may be benefit from this switching, but I am still not understanding it. [...] The idea is that any non blocking call is an opportunity to switch between greenlets. The monkey patching system will enable this. If you don't have any place in your greenlets that are monkey patched (and therefore allow switching), you can be a good citizen and drop in a patched sleep call so that other greenlets will get time to run. Otherwise, you'll simply hog the interpreter and no one else will be able to run. This the principle. I still don't understand your question. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] real use case of gevent context switch gevent.sleep(0)
On 2015-06-21 19:48, anu sree wrote: Thanks noufal, I got it. After gevent.spawn, we have to do join or gevent.joinall or gevent.sleep or patched library call to start the greenlets, right ? join (and joinall) will pause the current greenlet till the ones you've joined terminate (similar to the threading.Thread join method). gevent.sleep will suspend the execution of the current greenlet and let other greenlets get some time to execute. I have see the code which not using any of these after gevent.spawn, How it is working there ? https://github.com/Juniper/contrail-controller/blob/master/src/opserver/uveserver.py#L656 Neither of these are *necessary*. Just creating a greenlet and `.start`ing it will make it run similar to a thread as long as other greenlets don't hog the CPU and have some calls that can context switch. ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] real use case of gevent context switch gevent.sleep(0)
On Sun, Jun 21 2015, anu sree wrote: [...] This code has two pause (gevent.sleep(0)), in Consumer.start and Worker.run. Here control goes to Worker.run when Consumer.start pauses and Consumer.start gets control back when Worker.run pauses. There may be benefit from this switching, but I am still not understanding it. [...] The idea is that any non blocking call is an opportunity to switch between greenlets. The monkey patching system will enable this. If you don't have any place in your greenlets that are monkey patched (and therefore allow switching), you can be a good citizen and drop in a patched sleep call so that other greenlets will get time to run. Otherwise, you'll simply hog the interpreter and no one else will be able to run. This the principle. I still don't understand your question. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] real use case of gevent context switch gevent.sleep(0)
Hi Krace, I am still not understanding from code (practical example) where we are getting the benefit of PAUSE and let other greenlet to execute. I have created a simple worker program from your example https://github.com/dcramer/taskmaster/blob/79a312c5cb3c34d00829fe9cf4204aeb478a0166/src/taskmaster/client.py#L26 . This code has two pause (gevent.sleep(0)), in Consumer.start and Worker.run. Here control goes to Worker.run when Consumer.start pauses and Consumer.start gets control back when Worker.run pauses. There may be benefit from this switching, but I am still not understanding it. Please check the attached code and output. Is my example code valid ? CODE == import gevent class Worker(object): def __init__(self, consumer, target): self.consumer = consumer self.target = target def run(self): print Worker.run, START self.started = True while self.started: print Greenlet thread (from Worker.run) Pauses, START gevent.sleep(0) print Greenlet thread (from Worker.run) Pauses, END job = self.consumer.get_job() self.target(job) print Worker.run, END class Consumer(object): def __init__(self, target): self.target = target def start(self): print Consumer.start, START self.started = True worker = Worker(self, self.target) gevent.spawn(worker.run) while self.started: print Main Interpreter greenlet (from Consumer.start) Pauses, START gevent.sleep(0) print Main Interpreter greenlet (from Consumer.start) Pauses, END \n print Consumer.start, END def get_job(self): return 1 # def my_target_1(n): print my_target_1, START nums = [] while n 0: nums.append(n) n -= 1 print my_target_1, END c = Consumer(my_target_1) c.start() OUTPUT === Consumer.start, START Main Interpreter greenlet (from Consumer.start) Pauses, START Worker.run, START Greenlet thread (from Worker.run) Pauses, START Main Interpreter greenlet (from Consumer.start) Pauses, END Main Interpreter greenlet (from Consumer.start) Pauses, START Greenlet thread (from Worker.run) Pauses, END my_target_1, START my_target_1, END Greenlet thread (from Worker.run) Pauses, START Main Interpreter greenlet (from Consumer.start) Pauses, END Main Interpreter greenlet (from Consumer.start) Pauses, START Greenlet thread (from Worker.run) Pauses, END my_target_1, START my_target_1, END Greenlet thread (from Worker.run) Pauses, START Main Interpreter greenlet (from Consumer.start) Pauses, END ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers