max time threads

P

pacopyc

Hi, I'm trying to work with threads and I need your help. This is
code:

from threading import Thread
from Queue import Queue
import time
import random

def test_fun (k,q,t):
time.sleep(t)
print "hello world from thread " + str(q.get()) + " (sleep time =
" + str(t) + " sec.)"
q.task_done()

queue = Queue()
for i in range (1,10):
queue.put(i)
for j in range(queue.qsize()):
num = random.randint(1,30)
worker = Thread(target=test_fun, args=(j,queue,num))
worker.setDaemon(True)
worker.start()
queue.join()


Execution:

hello world from thread 1 (sleep time = 5 sec.)
hello world from thread 2 (sleep time = 5 sec.)
hello world from thread 3 (sleep time = 6 sec.)
hello world from thread 4 (sleep time = 8 sec.)
hello world from thread 5 (sleep time = 10 sec.)
hello world from thread 6 (sleep time = 13 sec.)
hello world from thread 7 (sleep time = 18 sec.)
hello world from thread 8 (sleep time = 19 sec.)
hello world from thread 9 (sleep time = 20 sec.)

Some questions for you:

1) Why order is always the same (thread 1, thread 2, thread 3 ....
thread 9) and also seconds are always increasing? I don't understand.
2) I'd like to decide a max time for each thread. If max time = 7 sec.
I want to print only threads with sleep time <= 7 sec. How can I do?
Can you modify my code?

Thank you very much
 
M

MRAB

pacopyc said:
Hi, I'm trying to work with threads and I need your help. This is
code:

from threading import Thread
from Queue import Queue
import time
import random

def test_fun (k,q,t):
time.sleep(t)
print "hello world from thread " + str(q.get()) + " (sleep time =
" + str(t) + " sec.)"
q.task_done()

queue = Queue()
for i in range (1,10):
queue.put(i)
for j in range(queue.qsize()):
num = random.randint(1,30)
worker = Thread(target=test_fun, args=(j,queue,num))
worker.setDaemon(True)
worker.start()
queue.join()


Execution:

hello world from thread 1 (sleep time = 5 sec.)
hello world from thread 2 (sleep time = 5 sec.)
hello world from thread 3 (sleep time = 6 sec.)
hello world from thread 4 (sleep time = 8 sec.)
hello world from thread 5 (sleep time = 10 sec.)
hello world from thread 6 (sleep time = 13 sec.)
hello world from thread 7 (sleep time = 18 sec.)
hello world from thread 8 (sleep time = 19 sec.)
hello world from thread 9 (sleep time = 20 sec.)

Some questions for you:

1) Why order is always the same (thread 1, thread 2, thread 3 ....
thread 9) and also seconds are always increasing? I don't understand.
2) I'd like to decide a max time for each thread. If max time = 7 sec.
I want to print only threads with sleep time <= 7 sec. How can I do?
Can you modify my code?

Thank you very much

1)

First it puts the numbers 1..9 into 'queue', then it starts 9 threads,
giving each a number 'num'.

Each thread waits for 'num' seconds ('t' in the thread).

The thread with the lowest value of 'num' wakes first, gets the first
entry from 'queue' (the value 1), and therefore prints "thread 1".

The thread with the second-lowest value of 'num' wakes next, gets the
second entry from 'queue' (the value 2), and therefore prints "thread
2".

And so on.

2)

If a thread is given a value of 'num' of more than a maximum, that
thread shouldn't print its output, but it should still get the entry
from the queue (assuming that you want it to still behave the same
otherwise).
 
P

pacopyc

1)

First it puts the numbers 1..9 into 'queue', then it starts 9 threads,
giving each a number 'num'.

Each thread waits for 'num' seconds ('t' in the thread).

The thread with the lowest value of 'num' wakes first, gets the first
entry from 'queue' (the value 1), and therefore prints "thread 1".

The thread with the second-lowest value of 'num' wakes next, gets the
second entry from 'queue' (the value 2), and therefore prints "thread
2".

And so on.

2)

If a thread is given a value of 'num' of more than a maximum, that
thread shouldn't print its output, but it should still get the entry
from the queue (assuming that you want it to still behave the same
otherwise).

Ok, the problem is that I want fix a time max for each thread. For
example run 10 threads (each can terminate its work in 10 sec. max
fixed time) and wait them. If they finish its work in < 10 sec. (for
example 2 sec.) very good ... go on immediately (don't wait
unnecessary time), but if a thread use more than 10 sec. stop wait (I
kill it) .... when all threads have finished their work or when they
have used all their available time (10 sec.) the program must go on
(don't wait).
 
M

MRAB

pacopyc said:
[snip]
Ok, the problem is that I want fix a time max for each thread. For
example run 10 threads (each can terminate its work in 10 sec. max
fixed time) and wait them. If they finish its work in < 10 sec. (for
example 2 sec.) very good ... go on immediately (don't wait
unnecessary time), but if a thread use more than 10 sec. stop wait (I
kill it) .... when all threads have finished their work or when they
have used all their available time (10 sec.) the program must go on
(don't wait).

It's not possible to kill a thread. If you want a thread to have a
maximum time, the thread must check occasionally how long it has been
running and terminate if necessary.

Another programming language (Java) originally had the ability to kill
threads, but that was later deprecated because it caused problems due to
not knowing what the thread was doing when it was killed (it might have
been in the middle of updating something at the time, for example,
leaving the system in an inconsistent state).
 
P

pacopyc

pacopyc wrote:

[snip]
Ok, the problem is that I want fix a time max for each thread. For
example run 10 threads (each can terminate its work in 10 sec. max
fixed time) and wait them. If they finish its work in < 10 sec. (for
example 2 sec.) very good ... go on immediately (don't wait
unnecessary time), but if a thread use more than 10 sec. stop wait (I
kill it) .... when all threads have finished their work or when they
have used all their available time (10 sec.) the program must go on
(don't wait).

It's not possible to kill a thread. If you want a thread to have a
maximum time, the thread must check occasionally how long it has been
running and terminate if necessary.

Another programming language (Java) originally had the ability to kill
threads, but that was later deprecated because it caused problems due to
not knowing what the thread was doing when it was killed (it might have
been in the middle of updating something at the time, for example,
leaving the system in an inconsistent state).

Ok, I understand. But is possible fix max wait time (for example 60
sec.) in main thread and check queue for task done and at the same
time the remaining time. When all task have done or wait time has
expired main thread go on. Is it possible? Can you write code?
Thank you very much.
 
M

MRAB

pacopyc said:
pacopyc wrote:

[snip]
Ok, the problem is that I want fix a time max for each thread. For
example run 10 threads (each can terminate its work in 10 sec. max
fixed time) and wait them. If they finish its work in < 10 sec. (for
example 2 sec.) very good ... go on immediately (don't wait
unnecessary time), but if a thread use more than 10 sec. stop wait (I
kill it) .... when all threads have finished their work or when they
have used all their available time (10 sec.) the program must go on
(don't wait).
It's not possible to kill a thread. If you want a thread to have a
maximum time, the thread must check occasionally how long it has been
running and terminate if necessary.

Another programming language (Java) originally had the ability to kill
threads, but that was later deprecated because it caused problems due to
not knowing what the thread was doing when it was killed (it might have
been in the middle of updating something at the time, for example,
leaving the system in an inconsistent state).

Ok, I understand. But is possible fix max wait time (for example 60
sec.) in main thread and check queue for task done and at the same
time the remaining time. When all task have done or wait time has
expired main thread go on. Is it possible? Can you write code?
Thank you very much.

The documentation says that queue.join() can't have a timeout, so you
might have to think of another way to achieve the same effect.

You could, for example, have a results queue into which a thread puts
something to indicate when it has finished, and then use the .get method
on it in the main thread (the .get method can have a timeout).
 
R

Raymond Hettinger

The join() method is all about waiting for all the tasks to be done. If you don't care whether the tasks have actually finished, you can periodically poll the unfinished task count:

stop = time() + timeout
while q.unfinished_tasks and time() < stop:
sleep(1)

This loop will exist either when the tasks are done or when the timeout period has elapsed.


Raymond


Hi, I am trying to work with threads and I need your help. This is
code:

from threading import Thread
from Queue import Queue
import time
import random

def test_fun (k,q,t):
time.sleep(t)
print "hello world from thread " + str(q.get()) + " (sleep time =
" + str(t) + " sec.)"
q.task_done()

queue = Queue()
for i in range (1,10):
queue.put(i)
for j in range(queue.qsize()):
num = random.randint(1,30)
worker = Thread(target=test_fun, args=(j,queue,num))
worker.setDaemon(True)
worker.start()
queue.join()


Execution:

hello world from thread 1 (sleep time = 5 sec.)
hello world from thread 2 (sleep time = 5 sec.)
hello world from thread 3 (sleep time = 6 sec.)
hello world from thread 4 (sleep time = 8 sec.)
hello world from thread 5 (sleep time = 10 sec.)
hello world from thread 6 (sleep time = 13 sec.)
hello world from thread 7 (sleep time = 18 sec.)
hello world from thread 8 (sleep time = 19 sec.)
hello world from thread 9 (sleep time = 20 sec.)

Some questions for you:

1) Why order is always the same (thread 1, thread 2, thread 3 ....
thread 9) and also seconds are always increasing? I do not understand.
2) I'd like to decide a max time for each thread. If max time = 7 sec.
I want to print only threads with sleep time <= 7 sec. How can I do?
Can you modify my code?

Thank you very much
1)

First it puts the numbers 1..9 into 'queue', then it starts 9 threads,
giving each a number 'num'.

Each thread waits for 'num' seconds ('t' in the thread).

The thread with the lowest value of 'num' wakes first, gets the first
entry from 'queue' (the value 1), and therefore prints "thread 1".

The thread with the second-lowest value of 'num' wakes next, gets the
second entry from 'queue' (the value 2), and therefore prints "thread
2".

And so on.

2)

If a thread is given a value of 'num' of more than a maximum, that
thread should not print its output, but it should still get the entry
from the queue (assuming that you want it to still behave the same
otherwise).
[snip]

it is not possible to kill a thread. If you want a thread to have a
maximum time, the thread must check occasionally how long it has been
running and terminate if necessary.

Another programming language (Java) originally had the ability to kill
threads, but that was later deprecated because it caused problems due to
not knowing what the thread was doing when it was killed (it might have
been in the middle of updating something at the time, for example,
leaving the system in an inconsistent state).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top