Gevent Greenlet log to file without joining ?

279 views
Skip to first unread message

Krishneil Singh

unread,
Sep 1, 2017, 2:25:39 PM9/1/17
to gevent: coroutine-based Python network library

hi

i have a gevent Greenlet running a fucntion that logs to file. Running the function directly logs to file but when i spawn with Greenlets it no longer logs to file. The only way i saw it logging was that when i joined the Greenlet. Is it possible to log to File without joining the Greenlet ?

e.g code

import gevent
from gevent import Greenlet
log = Log().File('/tmp/test1.log')

def test1():
    while True:
        log.info('log from func test1')

Thread1 = Greenlet.spawn(test1)



# my logging class to log to File for reference  
class Log():
    '''
    Log to file or stdout
    '''
    @classmethod
    def _setup_logger(cls):
        '''
        Setup initial logging setting
        '''
        log = logging.getLogger(__name__)
        log.setLevel(logging.DEBUG)
        return log
    @classmethod
    def _register_custom_logger_levels(cls, Level_num, Name):
        '''
        register_custom_logger_levels tag method
        '''
        logging.addLevelName(Level_num, Name)

        def Custom_Logger(cls, message, *args, **kws):
            cls._log(Level_num, message, args, **kws)

        setattr(logging.Logger, Name.lower(), Custom_Logger)
    @classmethod
    def Stdout(cls):

Jason Madden

unread,
Sep 1, 2017, 2:36:07 PM9/1/17
to gev...@googlegroups.com

> On Sep 1, 2017, at 13:23, Krishneil Singh <krishne...@gmail.com> wrote:
>
> i have a gevent Greenlet running a fucntion that logs to file. Running the function directly logs to file but when i spawn with Greenlets it no longer logs to file. The only way i saw it logging was that when i joined the Greenlet. Is it possible to log to File without joining the Greenlet ?

This sounds like a case of not yielding to the event loop. The greenlet you spawn will only run when the current greenlet yields to the event loop---joining the spawned greenlet is one way for that to happen (so that explains why it works when you join), and cooperative I/O is another way (often this is monkey-patched). If you keep doing things in the current greenlet (CPU tasks, non-cooperative I/O, etc), the spawned greenlet will never get a chance to run. In this way, greenlets are not like threads: threads use preemptive scheduling, meaning one can take over control at any time with no warning, but greenlets use cooperative scheduling, meaning you have to allow other greenlets to run---usually this happens automatically when performing I/O or using other gevent functions, but if you're not doing that you have to take responsibility for yielding yourself. More on this at this link and its following paragraphs: http://www.gevent.org/intro.html#cooperative-multitasking

> def test1():
> while True:
> log.info
> ('log from func test1')
>

I know this is just an example, but that's a classic example of a poor greenlet. It will never yield control to the event loop and allow other greenlets to run (assuming that `.info` is really just writing to a standard file). To let other greenlets run you would need to insert calls to, say, `gevent.sleep` on every iteration of the loop.

Jason
Reply all
Reply to author
Forward
0 new messages