I was looking around the time module. The time.sleep(no_of_seconds) sleeps the program for no_of_seconds seconds.
I don't think this is busy wait. It must be scheduling the process at a later time. Just curious of how this is working.! :)

You can look in the source code of timemodule.c in the python source. In linux, this is done by calling select() with NULL arguments.

It would be great if someone already knowing about it could give me a better answer. I always try to get my answers from the source code and am left dazzled by the code in C. (last time I was trying to see how long was implemented)

It would be great if someone already knowing about it could give me a better answer. I always try to get my answers from the source code and am left dazzled by the code in C. (last time I was trying to see how long was implemented)

Well, here is the C function that does the job (from timemodule.c in python 3.1.1)

static int
floatsleep(double secs)
{
/* XXX Should test for MS_WINDOWS first! */
#if defined(HAVE_SELECT) && !defined(__EMX__)
	struct timeval t;
	double frac;
	frac = fmod(secs, 1.0);
	secs = floor(secs);
	t.tv_sec = (long)secs;
	t.tv_usec = (long)(frac*1000000.0);
	Py_BEGIN_ALLOW_THREADS
	if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
#ifdef EINTR
		if (errno != EINTR) {
#else
		if (1) {
#endif
			Py_BLOCK_THREADS
			PyErr_SetFromErrno(PyExc_IOError);
			return -1;
		}
	}
	Py_END_ALLOW_THREADS
#elif defined(__WATCOMC__) && !defined(__QNX__)
	/* XXX Can't interrupt this sleep */
	Py_BEGIN_ALLOW_THREADS
	delay((int)(secs * 1000 + 0.5));  /* delay() uses milliseconds */
	Py_END_ALLOW_THREADS
#elif defined(MS_WINDOWS)
	{
		double millisecs = secs * 1000.0;
		unsigned long ul_millis;

		if (millisecs > (double)ULONG_MAX) {
			PyErr_SetString(PyExc_OverflowError,
					"sleep length is too large");
			return -1;
		}
		Py_BEGIN_ALLOW_THREADS
		/* Allow sleep(0) to maintain win32 semantics, and as decreed
		 * by Guido, only the main thread can be interrupted.
		 */
		ul_millis = (unsigned long)millisecs;
		if (ul_millis == 0 ||
		    main_thread != PyThread_get_thread_ident())
			Sleep(ul_millis);
		else {
			DWORD rc;
			ResetEvent(hInterruptEvent);
			rc = WaitForSingleObject(hInterruptEvent, ul_millis);
			if (rc == WAIT_OBJECT_0) {
				/* Yield to make sure real Python signal
				 * handler called.
				 */
				Sleep(1);
				Py_BLOCK_THREADS
				errno = EINTR;
				PyErr_SetFromErrno(PyExc_IOError);
				return -1;
			}
		}
		Py_END_ALLOW_THREADS
	}
#elif defined(PYOS_OS2)
	/* This Sleep *IS* Interruptable by Exceptions */
	Py_BEGIN_ALLOW_THREADS
	if (DosSleep(secs * 1000) != NO_ERROR) {
		Py_BLOCK_THREADS
		PyErr_SetFromErrno(PyExc_IOError);
		return -1;
	}
	Py_END_ALLOW_THREADS
#elif defined(PLAN9)
	{
		double millisecs = secs * 1000.0;
		if (millisecs > (double)LONG_MAX) {
			PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
			return -1;
		}
		/* This sleep *CAN BE* interrupted. */
		Py_BEGIN_ALLOW_THREADS
		if(sleep((long)millisecs) < 0){
			Py_BLOCK_THREADS
			PyErr_SetFromErrno(PyExc_IOError);
			return -1;
		}
		Py_END_ALLOW_THREADS
	}
#else
	/* XXX Can't interrupt this sleep */
	Py_BEGIN_ALLOW_THREADS
	sleep((int)secs);
	Py_END_ALLOW_THREADS
#endif

	return 0;
}

As you can see, the algorithm is to call a 'waiting' function in the underlying OS. If the system has a select function (GNU/linux for example), the main job is done by a call to C's select function select(0,0,0,0,&t); where t is a timeval structure defining a time interval. On the watcom compiler, a C function delay(milliseconds); does the job, in MS windows, a call WaitForSingleObject(hInterruptEvent, ul_millis); waits for a given number of milliseconds. In OS2 it is DosSleep(milliseconds) . In a plan9 and other systems, it is sleep(milliseconds or seconds) .

Now you only need to find the documentation for these low level C functions !

Edited 5 Years Ago by Gribouillis: n/a

Oh thanks, :)
Will take some time to see and understand the whole code. (Will need to remember the C things).

This question has already been answered. Start a new discussion instead.