プロセスを開いてコマンドを送信し、結果を取得し、これに基づいてさらにコマンドを実行する機能があります。どの段階でも、失敗して戻るか、最後に成功ステートメントを出力する可能性があります。現在、これはすべてメインスレッドで発生するため、これが発生している間、プログラムは停止します (約 6 分かかります)。このコードをバックグラウンドで実行するように変更し、最後に必要な 1 行を出力するにはどうすればよいですか?
ここにスニペットがあります:
def ran_network_listen(access_point_id):
# "read hnb" command, check if location has IP set.
proc = subprocess.Popen(cmd_rancli, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc_stdout = proc.communicate(ran_opt_read_hnb)[0]
parse = proc_stdout.split('\n')
list_of_aps = ""
for item in parse:
if access_point_id in item:
if "n/c" in item:
print "AP has no ip."
#return
else:
print item
list_of_aps += item
if not access_point_id in list_of_aps:
print "AP not registered"
return
//etc
まず、これを試しました:
t = Thread(target=ran_network_listen, args=(args.ap_id,))
t.start()
ただし、フォアグラウンドで実行されるだけではありません。