% python ./sleepy_random_threads.py
2015-02-20 00:38:23,547: 'a' created with a sleep time of 19 seconds
2015-02-20 00:38:23,548: 'c' created with a sleep time of 5 seconds
2015-02-20 00:38:23,548: 'b' created with a sleep time of 2 seconds
2015-02-20 00:38:23,550: 'e' created with a sleep time of 7 seconds
2015-02-20 00:38:23,550: 'd' created with a sleep time of 11 seconds
2015-02-20 00:38:23,551: 'g' created with a sleep time of 18 seconds
2015-02-20 00:38:23,551: 'f' created with a sleep time of 22 seconds
- Seven threads (named "a" through "g") are created and executed as daemons.
- each thread will sleep between 2 and 22 seconds
- sleep duration is determined before the thread is created
- each thread will emit an INFO message when it is created, telling the thread name and the sleep duration
- The threads will emit a pair of DEBUG messages and go to sleep for their
specified durations (before repeating)
- one message for the thread name and sleep duration
- one message for the thread name and the number of completed sleep cycles
- the DEBUG messages are written to the log file, but not the console
^C2015-02-20 00:39:02,539: Signal received to stop this script. Exiting gracefully.
2015-02-20 00:39:02,539: 'f' completed 1 sleep cycles.
2015-02-20 00:39:02,540: 'g' completed 2 sleep cycles.
2015-02-20 00:39:02,540: 'd' completed 3 sleep cycles.
2015-02-20 00:39:02,540: 'e' completed 5 sleep cycles.
2015-02-20 00:39:02,541: 'b' completed 19 sleep cycles.
2015-02-20 00:39:02,541: 'c' completed 7 sleep cycles.
2015-02-20 00:39:02,541: 'a' completed 2 sleep cycles.
2015-02-20 00:39:02,541: Everything finished.
- When an exit signal is captured, the signal handler will emit a WARNING message then script will begin to exit
- When each thread stops, it will emit an INFO message telling the thread name and the number of completed sleep cycles
- When all theads have stopped, the script will emit an "everything finished" INFO message
Note: you can also
kill
the PID of the script in another window.
Use j
and k
to scroll down and up. Use q
to quit.
% less -FX log_random_threads.log
2015-02-20 00:38:23,547 - INFO - __init__: 'a' created with a sleep time of 19 seconds
2015-02-20 00:38:23,548 - DEBUG - run: 'a' is sleeping for 19 seconds.
2015-02-20 00:38:23,548 - DEBUG - run: 'a' completed 0 sleep cycles.
2015-02-20 00:38:23,548 - INFO - __init__: 'c' created with a sleep time of 5 seconds
2015-02-20 00:38:23,548 - DEBUG - run: 'c' is sleeping for 5 seconds.
2015-02-20 00:38:23,548 - INFO - __init__: 'b' created with a sleep time of 2 seconds
2015-02-20 00:38:23,549 - DEBUG - run: 'c' completed 0 sleep cycles.
2015-02-20 00:38:23,550 - DEBUG - run: 'b' is sleeping for 2 seconds.
2015-02-20 00:38:23,550 - INFO - __init__: 'e' created with a sleep time of 7 seconds
2015-02-20 00:38:23,550 - DEBUG - run: 'b' completed 0 sleep cycles.
...
Notice that the log file has a different format than the console output.