Im trying to return a "snapshot" of information from a function running in a thread with python. I thought it would be easy, but google doesent make any sens.
import thread
import sys
import time
def counter():
count = 0
while 1:
count = count +1
# Hi screen
print('Welcome to thread example!\n')
# Avalibel commands
print('Enter [quit] to exit. enter [status] for count status')
C = thread.start_new_thread(counter ,())
while 1:
try:
command = raw_input('Command: ')
if command == 'quit':
sys.exit()
elif command == 'status':
print(time.ctime())
print(C.count + '\n')
else:
print('unknown command. [quit] or [satus]')
except KeyboardInterrupt:
print "\nKeybord interrupt, exiting gracefully anyway."
sys.exit()
This above example gives me AttributeError: 'int' object has no attribute 'count', but i have tried a few "solutions" with different no success.
In this example i want counter() to run until i enter quit. A little output example:
Welcome to thread example!
Enter [quit] to exit. enter [status] for count status
>>> Command: status
Thu Feb 25 09:42:43 2016
123567
>>> Command: status
Thu Feb 25 10:0:43 2016
5676785785768568795
Question:
How do i return a "snapshot" value from
def counter?If i let this run for a couple of hours, will i have a memory issue?
check_output- as I know it is a good way to avoid issues with GIL that may cause longer execution.