Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tail a file with gevent

Tags:

python

gevent

I have a problem constructing gevent tail function. In general, the code works when I comment gevent.sleep in loop, but then CPU utilization is 100%. When I leave gevent.sleep program works but nothing is happening. Gevent version is 1.0b1.

import os
import gevent

def follow(filename):
    fd = os.open(filename, os.O_RDONLY|os.O_NONBLOCK)
    os.lseek(fd, 0, os.SEEK_END)
    hub = gevent.get_hub()
    watcher = hub.loop.io(fd, 1)
    while True:
        hub.wait(watcher)
        lines = os.read(fd, 4096).splitlines()
        if not lines:
            #gevent.sleep(.1)
            continue
        else:
            for line in lines:
                print "%s:%s" % (filename, line)

    os.close(fd)

if __name__ == '__main__':
    job1 = gevent.spawn(follow, '/var/log/syslog')
    job2 = gevent.spawn(follow, '/var/log/messages')

    gevent.joinall([job1, job2])
like image 843
inquisitor Avatar asked Mar 18 '12 21:03

inquisitor


People also ask

What is gevent in Python?

What is gevent From the official sitedescription: gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop. The description is rather obscure for those who are unfamiliar with the mentioned dependencies like greenlet, libev, or libuv.

What is the tail file in PowerShell?

PowerShell tail file – Windows tail command Updated on: September 29, 2020 Sarav AK PowerShell is a powerful tool and it enables the Windows servers to compete with the Shell and Linux command line features. One of the useful command in linux is tail which helps us to view the file as it gets updated mostly used to view the log files.

Does the tail command work with binary files?

The tail command works with plain text formats. It does not read binary files. So does this mean the tail command is a solution in search of a problem? Does it still have anything to offer? There’s more to the tail command than showing updates in real-time.

How do I tail a file in Linux?

The tail command is both quick and simple to use. While less is more, if you want to do more than simply follow a file (e.g., scroll and search), you may need less. By pressing Shift-F, you will be taken to the end of the file and will see new contents constantly. How Do You Tail Multiple Files In Linux?


2 Answers

Starting with gevent 1.0b2 you can use stat watchers to get notifications on file changes.

See the code and the libev documentation for stat watchers.

like image 61
Denis Avatar answered Sep 30 '22 00:09

Denis


Well, that's code doesn't 'tail' the file, it's just prints the whole file, BUT it's show's how 'loop.stat' works. It wait's for file to change - or be just touched, and then print's content. When it's wait - it take's almost no resources!

import gevent,os

def follow(filename):
    hub = gevent.get_hub()
    watcher = hub.loop.stat(filename)
    while True:
        hub.wait(watcher)
        with open(filename) as f:
            print f.read()

if __name__ == '__main__':
    jobs=[gevent.spawn(follow,'/var/log/syslog')]
    jobs+=[gevent.spawn(follow,'/var/log/messages')]
    gevent.joinall(jobs)
like image 21
Josh C Josh C Avatar answered Sep 30 '22 00:09

Josh C Josh C