Giter VIP home page Giter VIP logo

shellpydaemon's Introduction

What it is

This daemon provides a way to run shell commands in python without forking the python process. This is useful when your main python process is memory-intensive. The daemon reads shell commands from a file and runs them. There is no sanitization or safety checking.

How it works

On posix-compliant systems, files have advisory lock attributes that allow processes to claim access to them. Every so often, the daemon attempts to acquire a lock on a commands file. If the file isn't empty, the daemon interprets the first line in it as a shell command and runs it. The daemon then empties the commands file. The daemon then acquires a lock on a results file and writes the return code to the first line in that file. Finally the daemon releases both locks.

At the same time, your code uses the submit_command method to send a command to the daemon. This method, defined in helper.py, attempts to acquire a lock on the commands file. Once it has a lock, it writes the specified command to the first line of that file. It then releases its lock on the file. Next, it attempts to acquire a lock on the results file. Once it succeeds, it checks if there are any results in the first line. If there are not, it gives up the lock, waits, tries to re-acquire the lock, and checks again. This repeats until there is a result in the results file, at which point submit_command empties the results file and returns the result.

In the above all lock acquisition is done within a loop with a sleep statement. If a lock is not acquired, the relevant method waits an amount of time specified by the sleep_time argument that both the daemon and submit_command accept. It then tries again.

Usage

To run the daemon, use

python daemon.py path_to_results_file path_to_commands_file sleep_time

You can then issue commands to the daemon using the submit_command function provided by helper.py. As in,

from helper import submit_command
submit_command('ls -lah', 'path_to_commands_file', 'path_to_results_file', sleep_time)

The sleep_time is specified in seconds. Paths may be relative or absolute, but must be the same for both the submitter and the daemon.

shellpydaemon's People

Contributors

adamjermyn avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.