2017-09-07 67 views
0

ループの状態を取得するmysqlクエリを送信するループで関数を呼び出そうとしています。次に、ループ内でステータスが5の場合は、ループを中断します。関数はLoopで呼び出されていません - Python

ステータスは、ジョブをキャンセルするWebページ上のボタンから変更されます。 phpスクリプトは、MySQLデータベースを5に挑戦的に更新しますが、pythonスクリプトではそれを0としか見ません.0がデフォルト値です。つまり、ジョブが実行中または実行待ちです。

私の関数(docheck)が、実行してデータベースから番号5を取得するのではなく、変数チェックを0の値に保つ理由を知ることができます。

import pysftp 
import pexpect 
import sys 
import MySQLdb 
import os 
from pexpect import pxssh 

# Establish a MySQL connection 
con = MySQLdb.connect (host="localhost", user = "root", passwd = "R42chlA!", db = "BT") 

def getTest_Cases(): 
    cur = con.cursor() 
    cur.execute("Select * from Test_Cases") 
    row = cur.fetchall() 
    if row is not None: 
     return row 
    else: 
     return None 

def getNextComputeID(): 
    cur = con.cursor() 
    cur.execute("Select job_ID from Compute_jobs where status = 0 order by created_time ASC limit 1") 
    row = cur.fetchone() 
    if row is not None: 
     return row[0] 
    else: 
     return None 

def getNextFile(): 
    cur = con.cursor() 
    cur.execute("Select sheet from Compute_jobs where job_ID = '%s' order by created_time ASC limit 1" %jobid) 
    row = cur.fetchone() 
    if row is not None: 
     return row[0] 
    else: 
     return None 

def getRowsinOutTable(): 
    cur = con.cursor() 
    cur.execute("SELECT COUNT(*) FROM %s_output;" %tablename) 
    row = cur.fetchone() 
    if row is not None: 
     return row[0] 
    else: 
     return None 

def docheck(): 
    cur = con.cursor() 
    cur.execute("Select status from Compute_jobs where job_ID = \"%s\" order by created_time ASC limit 1" %jobid) 
    row = cur.fetchone() 
    if row is not None: 
     return row[0] 
    else: 
     return None 

jobid = getNextComputeID() 
filename = getNextFile() 
tablename = filename[:-3] 

srv = pysftp.Connection(host="10.52.117.175", username="root", password="[password]") 
srv.put("caseid/%s" %filename, "/home/enduser/%s" %filename) 
srv.close() 

ssh = pxssh.pxssh() 
ssh.login('10.52.117.175', 'root', '[password]') 
fout = file('logs/%s_%s.txt' % (tablename, jobid),'w') 
ssh.logfile = fout 
ssh.sendline("cd /home/enduser/") 
ssh.expect("enduser") 
ssh.sendline("./run_jobs %s" %filename) 
while True: 
    try: 
     ssh.expect("\n") 
     output = ssh.before 
     output = output.strip() 
     output = output.replace("\"", "'") 
     print(output) 
     checked = docheck() 
     print(checked) 
     print(jobid) 
     print "-----------------------------------------------------------------" 
     if checked == "5": 
      print "job cancelled!" 
      ssh.close 
      break 
    except: 
     ssh.close  
     break 


checked = docheck() 
print(checked) 
print(docheck()) 
ssh.close  
+0

変数 'checked'を意味しますか(あなたは「check」と言っていますか?) – cdarke

+0

ええ、関数はdocheckです –

+0

'docheck()'関数で 'row'(最初の要素だけでなく)の値を追跡することをお勧めしますか?問題を再現するためにコードを試す方法はありません。 – cdarke

答えて

0

breakのみループからの出口を引き​​起こすので、ループ後の任意のステートメントが実行されます。 一方、returnは、現在の関数からの終了を引き起こすので、この関数の中のさらなるステートメントは実行されません。

したがって、最初の要素を見つけて現在の関数を終了する場合は、 returnを使用してください。 この機能で実行を継続する場合は、breakを使用してください。

Btwコードのどこにでも+1が表示されません。では、毎回5を増やす方法は?

関連する問題