Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downside of using patched threading vs native gevent greenlets?

My understanding is that once I have called gevent.monkey.patch_all(), the standard threading module is modified to use greenlets instead of python threads. So if I write my application in terms of python threads, locks, semaphores etc, and then call patch_all, am I getting the full benefit of gevent, or am I losing out on something compared with using the explicit gevent equivalents?

The motivation behind this question is that I am writing a module which uses some threads/greenlets, and I am deciding whether it is useful to have an explicit switch between using gevent and using threading, or whether I can just use threading+patch_all without losing anything.

To put it in code, is this...

def myfunction():
  print 'ohai'

Greenlet.spawn(myfunction)

...any different to this?

import gevent.monkey
gevent.monkey.patch_all()
def mythread(threading.Thread):
  def run(self):
    print 'ohai'

mythread().start()
like image 719
kdt Avatar asked Nov 04 '22 18:11

kdt


1 Answers

At least your will loose some of greenlet-specific methods: link, kill, join etc. Also you can't use threads with, for example, gevent.pool module, that can be very useful. And there is a very little overhead for creating Thread object.

like image 116
Fedor Gogolev Avatar answered Nov 13 '22 18:11

Fedor Gogolev