How to stop running my threads after a period of time?












5















I need to stop running my threads after a period of time, In this example I put only 120 seconds. I try by using this methods by it does not work.



from threading import Thread
from Queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

#while True :


def OpenWSN ():
os.system("./toto")

def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")


def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())


q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()

#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")









share|improve this question




















  • 1





    What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

    – Jaxi
    Nov 21 '18 at 16:47











  • @Jaxi I put my timeout = 120

    – Sony
    Nov 21 '18 at 16:53











  • It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

    – OpalApps
    Nov 21 '18 at 19:03











  • As for the timeout - you are not using it, as log as I understand your code.

    – OpalApps
    Nov 21 '18 at 19:10
















5















I need to stop running my threads after a period of time, In this example I put only 120 seconds. I try by using this methods by it does not work.



from threading import Thread
from Queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

#while True :


def OpenWSN ():
os.system("./toto")

def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")


def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())


q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()

#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")









share|improve this question




















  • 1





    What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

    – Jaxi
    Nov 21 '18 at 16:47











  • @Jaxi I put my timeout = 120

    – Sony
    Nov 21 '18 at 16:53











  • It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

    – OpalApps
    Nov 21 '18 at 19:03











  • As for the timeout - you are not using it, as log as I understand your code.

    – OpalApps
    Nov 21 '18 at 19:10














5












5








5


1






I need to stop running my threads after a period of time, In this example I put only 120 seconds. I try by using this methods by it does not work.



from threading import Thread
from Queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

#while True :


def OpenWSN ():
os.system("./toto")

def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")


def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())


q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()

#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")









share|improve this question
















I need to stop running my threads after a period of time, In this example I put only 120 seconds. I try by using this methods by it does not work.



from threading import Thread
from Queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

#while True :


def OpenWSN ():
os.system("./toto")

def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")


def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())


q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()

#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")






python shell python-multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:25









OpalApps

719




719










asked Nov 21 '18 at 16:09









SonySony

668




668








  • 1





    What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

    – Jaxi
    Nov 21 '18 at 16:47











  • @Jaxi I put my timeout = 120

    – Sony
    Nov 21 '18 at 16:53











  • It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

    – OpalApps
    Nov 21 '18 at 19:03











  • As for the timeout - you are not using it, as log as I understand your code.

    – OpalApps
    Nov 21 '18 at 19:10














  • 1





    What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

    – Jaxi
    Nov 21 '18 at 16:47











  • @Jaxi I put my timeout = 120

    – Sony
    Nov 21 '18 at 16:53











  • It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

    – OpalApps
    Nov 21 '18 at 19:03











  • As for the timeout - you are not using it, as log as I understand your code.

    – OpalApps
    Nov 21 '18 at 19:10








1




1





What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

– Jaxi
Nov 21 '18 at 16:47





What do you mean by "it does not work" you're not referencing timeout nor timeout_start for one..

– Jaxi
Nov 21 '18 at 16:47













@Jaxi I put my timeout = 120

– Sony
Nov 21 '18 at 16:53





@Jaxi I put my timeout = 120

– Sony
Nov 21 '18 at 16:53













It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

– OpalApps
Nov 21 '18 at 19:03





It does not work (I take that you simply don't see anything in console) because you have written from Queue import Queue instead of from queue import Queue.

– OpalApps
Nov 21 '18 at 19:03













As for the timeout - you are not using it, as log as I understand your code.

– OpalApps
Nov 21 '18 at 19:10





As for the timeout - you are not using it, as log as I understand your code.

– OpalApps
Nov 21 '18 at 19:10












1 Answer
1






active

oldest

votes


















0














I think this is what you are trying to achieve:



import threading
from queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")

def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")

def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())

q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()

cv = threading.Condition()
cv.acquire()
cv.wait( timeout )

print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )

os.system("echo -exit-")


This produces the following output:



C:tempStackExchangeStopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-


What is happening there - you are starting two threads, each starts separate process in the system. After the said threads were started, you return to your main thread, allocate a "lock" and wait until this lock is signaled, or time out takes place.
In this particular case nobody signals the lock, so the only chance to finish the application is to wait until the time out happens.
I would extend your application that it signals the lock in each thread function, so we can terminate the main thread only if both of thread functions terminate.
But that was not the part of your question, so I assume you can leave without signalling.






share|improve this answer


























  • I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

    – Sony
    Nov 22 '18 at 12:59











  • It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

    – OpalApps
    Nov 22 '18 at 15:14













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416125%2fhow-to-stop-running-my-threads-after-a-period-of-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think this is what you are trying to achieve:



import threading
from queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")

def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")

def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())

q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()

cv = threading.Condition()
cv.acquire()
cv.wait( timeout )

print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )

os.system("echo -exit-")


This produces the following output:



C:tempStackExchangeStopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-


What is happening there - you are starting two threads, each starts separate process in the system. After the said threads were started, you return to your main thread, allocate a "lock" and wait until this lock is signaled, or time out takes place.
In this particular case nobody signals the lock, so the only chance to finish the application is to wait until the time out happens.
I would extend your application that it signals the lock in each thread function, so we can terminate the main thread only if both of thread functions terminate.
But that was not the part of your question, so I assume you can leave without signalling.






share|improve this answer


























  • I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

    – Sony
    Nov 22 '18 at 12:59











  • It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

    – OpalApps
    Nov 22 '18 at 15:14


















0














I think this is what you are trying to achieve:



import threading
from queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")

def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")

def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())

q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()

cv = threading.Condition()
cv.acquire()
cv.wait( timeout )

print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )

os.system("echo -exit-")


This produces the following output:



C:tempStackExchangeStopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-


What is happening there - you are starting two threads, each starts separate process in the system. After the said threads were started, you return to your main thread, allocate a "lock" and wait until this lock is signaled, or time out takes place.
In this particular case nobody signals the lock, so the only chance to finish the application is to wait until the time out happens.
I would extend your application that it signals the lock in each thread function, so we can terminate the main thread only if both of thread functions terminate.
But that was not the part of your question, so I assume you can leave without signalling.






share|improve this answer


























  • I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

    – Sony
    Nov 22 '18 at 12:59











  • It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

    – OpalApps
    Nov 22 '18 at 15:14
















0












0








0







I think this is what you are trying to achieve:



import threading
from queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")

def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")

def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())

q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()

cv = threading.Condition()
cv.acquire()
cv.wait( timeout )

print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )

os.system("echo -exit-")


This produces the following output:



C:tempStackExchangeStopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-


What is happening there - you are starting two threads, each starts separate process in the system. After the said threads were started, you return to your main thread, allocate a "lock" and wait until this lock is signaled, or time out takes place.
In this particular case nobody signals the lock, so the only chance to finish the application is to wait until the time out happens.
I would extend your application that it signals the lock in each thread function, so we can terminate the main thread only if both of thread functions terminate.
But that was not the part of your question, so I assume you can leave without signalling.






share|improve this answer















I think this is what you are trying to achieve:



import threading
from queue import Queue
import os
import time

timeout = 120 # [seconds]

timeout_start = time.time()

def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")

def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")

def wrapper1(func, queue):
queue.put(func())

def wrapper2(func, queue):
queue.put(func())

q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()

cv = threading.Condition()
cv.acquire()
cv.wait( timeout )

print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )

os.system("echo -exit-")


This produces the following output:



C:tempStackExchangeStopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-


What is happening there - you are starting two threads, each starts separate process in the system. After the said threads were started, you return to your main thread, allocate a "lock" and wait until this lock is signaled, or time out takes place.
In this particular case nobody signals the lock, so the only chance to finish the application is to wait until the time out happens.
I would extend your application that it signals the lock in each thread function, so we can terminate the main thread only if both of thread functions terminate.
But that was not the part of your question, so I assume you can leave without signalling.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 19:36

























answered Nov 21 '18 at 19:29









OpalAppsOpalApps

719




719













  • I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

    – Sony
    Nov 22 '18 at 12:59











  • It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

    – OpalApps
    Nov 22 '18 at 15:14





















  • I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

    – Sony
    Nov 22 '18 at 12:59











  • It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

    – OpalApps
    Nov 22 '18 at 15:14



















I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

– Sony
Nov 22 '18 at 12:59





I validate you answer. because it is correct, I have the wright execution. My only problem now is about the last instruction; instead of os.system("echo -exit-") I put os.system("echo 'quitn' ") However it does not work. The execution of the program does not stop

– Sony
Nov 22 '18 at 12:59













It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

– OpalApps
Nov 22 '18 at 15:14







It should be "exit", not "quit". os.system( <your command here>) invokes a command interpreter as if you were typing these commands in your command shell. Whatever you try to execute - test first by just running it in a command shell. Could be also useful - return value of os.system() call is what that command/program returns after it ends (program exist status). That is as if you were checking the ERRNO variable in the command shell after some command execution. It gives you an idea if and what went wrong.

– OpalApps
Nov 22 '18 at 15:14






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416125%2fhow-to-stop-running-my-threads-after-a-period-of-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith