Multiple threads

Henrik Faber hfaber at invalid.net
Wed Nov 16 09:07:48 EST 2011


On 16.11.2011 14:48, Eduardo Oliva wrote:

>   I need my script to run 2 separated threads, and then when the first has finished, starts the next one....but no more than 2 threads.
>   I know that Semaphores would help with that.
>   But the problem here is to know when the thread has finished its job, to release the semaphore and start another thread.

Absolute standard request, has nothing to do with Python. The way to go
(in Cish pseudocode) is:

thread() {
	/* do work */
	[...]

	/* finished! */
	semaphore++;
}

semaphore = 2
while (jobs) {
	semaphore--; 	// will block if pool exhausted
	thread();
}

// in the end, collect remaining two workers
semaphore -= 2		// will block until all are finished


Best regards,
Henrik



More information about the Python-list mailing list