Wednesday 24 July 2013

threading, thread and _thread

thread is the "old school" module for threading, threading is the "new way". _thread is the implementation of threading in Python 3.0.  It was so-named in Py3K to label it as an implementation "detail".

using time module with thread module

import thread; thread.start_new_thread( my_function, (args in a tuple structure).

Then inside my_function,time.sleep(x) to call the grist of the function at regular intervals from within a loop (e..g while true; or while count < x:).

Threads are sometimes called light-weight processes. They do not require as much memory overhead as a regular process.

time in python

If you are creating multi-threaded programs in Python (and, let's face it, who isn't) you need a familiarity with the time module. The time module is always there, but some functions may only work on certain platforms.

time.time() - is the basic thing you need to know. It returns the number of seconds since the "epoch". The epoch is when time starts. On Jan 1 of that year at time 0, the "time since epoch" is zero. On Unix, and in chances are if you try this on Windows too, the epoch is 1970.

time.gmtime(0) - returns the epoch.

time.ctime(xx) - converts a time since the epoch into local time. This will show day, month year and time when printed.

time.sleep(s) - suspends execution for a given number of seconds.