I am trying to get rows for a particular machine name from v$session and spawning a child thread which calls a function called kill_it() to work on its particular row. And the main do_this() sleeps for 100sec and checks for newer rows.
def do_this():
sql='select * from v$session where machine like abc'
session=Popen(['sqlplus','-S','/ as sysdba'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
session.stdin.write(sql)
stdout,stderr=session.communicate()
stdout=stdout.strip()
stdout=stdout.split('\n')
for i in range (len(stdout)):
if 'no rows selected' not in stdout:
sql_output=stdout[i].split(',')
client_info=sql_output[0]
sid=sql_output[1]
serial=sql_output[2]
program=sql_output[3]
last_call=sql_output[4]
process=sql_output[5]
machine=sql_output[6]
t = Thread(target=kill_it, args=(client_info,sid,serial,program,last_call,process,machine,))
print t
t.start()
while True:
do_this()
time.sleep(100)
But in the kill_it() function which should run in its own thread, but when I am trying to put the child thread to sleep for 10secs, it is instead sleeping for 100secs or even if I remove the sleep it doesnt really keep looking for xyz in client info.
def kill_it(client_info,sid,serial,program,last_call,process,machine):
while True:
print "thread is :"+str(current_thread())
last_call=int(last_call)
if 'xyz' in client_info and last_call>100 :
command='alter system kill session \''+sid+','+serial+'\' immediate;'
print command
'''
session=Popen(['sqlplus','-S','/ as sysdba'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
session.stdin.write(command)
stdout,stderr=session.communicate()
print stdout
print stderr
'''
print 'done'
break;
else:
print 'Sleeping coz job didn`t run for more than 5mins'
time.sleep(10)
WOrks as expected when there is one row but for more than one gives issues.