Skip to main content

Simple Task Scheduler & Executor with Friendly Text User Interface

Project description

gtui

Task scheduler & executor with text-based user interface, able to:

  • declare task & dependency as a graph
  • execute task graph
  • show task status, stdout & log
  • provide a nice user interface

To install:

$ pip install gtui

Quickstart

Let's say we have some helloworld tasks & their dependencies like this:

task_graph_demo.png

We need to create Task and add them to TaskGraph:

import time
import logging
from gtui import Task, TaskGraph

def foo(n):
    logging.info('foo(%s) is called', n)
    print('Start to sleep %s seconds.' % n)
    time.sleep(n)
    print('Hello World!')

t1 = Task('t1', func=foo, args=[0.1])
t2 = Task('t2', func=foo, args=[1])
t3 = Task('t3', func=foo, args=[1])
t4 = Task('t4', func=foo, args=[0.5])

g = TaskGraph()
g.add_task(t1)
g.add_task(t2, waiting_for=t1)
g.add_task(t3, waiting_for=t1)
g.add_task(t4, waiting_for=[t2, t3])

g.run()

TaskGraph.run starts the text user interface, and you can navigate through tasks, see their status, output and logs: tui_demo.gif

Keybindings:

  • t : toggle tail -f mode, will follow text when enabled
  • tab : switch between output & log
  • j/k : select previous/next task
  • h/l : page up/down
  • ↑/↓ : scroll up/down one line
  • y : copy text
  • q : exit

Task & TaskGraph

Task defines what to do and has an unique name:

# foo(*args, **kwargs) will be called
t = Task(name='foo', func=foo, args=[1, 2], kwargs={'foo': 'bar'})

TaskGraph defines execution order of a set of tasks, it provides method to declare task & dependency:

g = TaskGraph()
g.add_task(t1)                              # added task t1
g.add_task(t2, waiting_for=t1)              # added task t2, t2 runs after t1 finishes
g.add_tasks([t3, t4])                       # added task t3, t4
g.add_dependency(t3, waiting_for=[t1, t2])  # declare t3 to run after t1 & t2 finish

When TaskGraph contains a cycle denpendency, run method will throw a ValueError. You can also use has_cycle to check:

> g = TaskGraph()
> g.add_tasks([t1, t2])
> g.add_dependency(t1, waiting_for=t2)
> g.add_dependency(t2, waiting_for=t1)
> g.has_cycle()
[t1, t2, t1]

Run Options

TaskGraph.run provides some options:

g.run(
  title='Demo',           # Text shown at the left bottom corner
  callback=None,          # A function called when execution fail or succeed
  log_formatter=None,     # An instance of logging.Formatter, to specify the log format
  exit_on_success=False   # whether exit tui when execution succeed
)

callback can be used to notify the execution result, it will be called with an boolean indicating whether execution succeed. gtui.callback has some common callbacks:

# emit a desktop notification, use osascript on mac and notify-send on linux
from gtui import callback
g.run(callback.desktop_nofity(title='Plz See Here!', success_msg='Success', fail_msg='Fail'))

Possible Problem with Stdout

Writing to stdout will break the TUI display. gtui runs each task in a new thread with sys.stdout replaced so functions like print will just work fine. When creating a new thread inside a task, gtui.IORedirectedThread can be used to achieve the same result:

from gtui import IORedirectedThread

t = IORedirectedThread(target=print, args=['hello world'])
t.start()
t.join()

content = t.get_stdout_content()

However, gtui doesn't try to deal with other cases so you should take care of it by yourself.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gtui-0.1.1.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

gtui-0.1.1-py2.py3-none-any.whl (29.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gtui-0.1.1.tar.gz.

File metadata

  • Download URL: gtui-0.1.1.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.16

File hashes

Hashes for gtui-0.1.1.tar.gz
Algorithm Hash digest
SHA256 63eff172e1c9560576c32b611bc2dd8ef3111b90485b5cd9608a5bf91aa2af70
MD5 95ac5afdc2a50518a099280fb9c1bb42
BLAKE2b-256 48638f087eda220c8289e68937f7284eaf1a2799d1554f27c69a65297599b749

See more details on using hashes here.

File details

Details for the file gtui-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: gtui-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.16

File hashes

Hashes for gtui-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1a1b0998dd1944dd5a89faee8475a48e80905fe740b7b270f1e66f6804a25c21
MD5 87b6fadb97bb19fb92b6497412979d99
BLAKE2b-256 1177a8960565042eac79f5901944682cc104d3776a57e0bd0a5a754367ab5a52

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page