首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打印变量后使用subprocess.call?

如何打印变量后使用subprocess.call?
EN

Stack Overflow用户
提问于 2018-11-25 02:16:12
回答 1查看 48关注 0票数 0

在我的程序中,我使用subprocess.call自动运行apache drill。在此之后,我进行了一些查询,并希望打印结果。在编写代码以自动运行apache drill之前,我是手动执行的,我可以打印结果,但现在我不能这样做。我的最后一次尝试是在一个文件中写入,但行为是相同的,没有写入任何内容。我的代码如下。

代码语言:javascript
复制
import subprocess
from pydrill.client import PyDrill
import sys
writer = open('resultado.txt', 'w')
cmdmsg = subprocess.check_output("cd C:\\Users\\Tito\\Downloads\\apache-drill-1.14.0\\bin & sqlline -u \"jdbc:drill:zk=local\"", shell = True)
writer.write("teste de msg: " + str(cmdmsg))
drill = PyDrill(host='localhost', port=8047)
if drill.is_active:
    result = drill.query('''SELECT * FROM cp.`employee.json` LIMIT 3''')
    result2 = drill.query('''SELECT * FROM dfs.`/Users/Tito/Desktop/banco_gal.csv` LIMIT 5''')
    for tuple in result2:
        writer.write(tuple)
writer.close
EN

回答 1

Stack Overflow用户

发布于 2018-12-03 04:36:51

我可以解决这个问题。对这个话题来说,有三件事很重要。a)在打开apache drill之后,我们应该杀死java虚拟机和shell。b)窗口缓冲区非常短,所以没有打印结果。c)对于此任务,popen方法比call更好。

代码语言:javascript
复制
import os
import re
import subprocess
import traceback
from os import path
from pydrill.client import PyDrill

DRILL_HOST = 'localhost'
DRILL_PORT = 8047

JAVA_HOME = ''
JAVA_HOME = JAVA_HOME or ("JAVA_HOME" in os.environ and os.environ["JAVA_HOME"])


JPS_EXE_PATH = path.join(JAVA_HOME, 'bin', 'jps.exe')

KILL_DRILL_JVM = True

def getJvmPID(className):
    pid = None
    print('Running JPS cmd: %s' % JPS_EXE_PATH)
    jpsOutput = subprocess.check_output(JPS_EXE_PATH)
    jpsOutput = str(jpsOutput)
    se = re.search(r'([0-9]*\s*)' + className, jpsOutput)
    if se:
        pid = se.group(1)
    return pid

def killProcessByPID(pid):
    killCmd = ['taskkill', '/f', '/pid', str(pid)]
    print('Running taskkill cmd: %s' % killCmd)
    killCmdOuput = subprocess.check_output(killCmd, stderr=subprocess.STDOUT)
    print(str(killCmdOuput))

def killJvm(className):
    pid = getJvmPID(className)
    killProcessByPID(pid)


drillBinDir    = 'C:/Users/Tito/Downloads/apache-drill-1.14.0/bin'

sqlinePath   = path.join(drillBinDir, 'sqlline.bat')

drillCmdList = [sqlinePath , '-u', '"jdbc:drill:zk=local"']
drillCmdStr = " ".join(drillCmdList)
drillConAttempts = 2
while drillConAttempts > 0:
        drillConAttempts -= 1
        print("Connecting to drill on %s:%d..." % (DRILL_HOST, DRILL_PORT))
        try:
            drill = PyDrill(host=DRILL_HOST, port=DRILL_PORT)
        except:
            print("Exception when creating object")
            traceback.print_exc()

        print("Checking Drill conection...")
        try:
            if drill.is_active():
                print("Connected.")
                break
            elif drillConAttempts > 0:
                print("Could not connect to Drill. Trying to start Drill...")
                print("Running cmd '%s > %s'" % (drillCmdStr, os.devnull) )
                devNull = open(os.devnull,"w")
                cmdProc = subprocess.Popen(drillCmdStr, cwd=drillBinDir, stdout=devNull, stderr=subprocess.STDOUT, shell=True)
                print("Started CMD process with PID %d" %(cmdProc.pid))
        except:
            print("Exception when checking connection")
            traceback.print_exc()



if drill.is_active():
    result = drill.query('''SELECT * FROM cp.`employee.json` LIMIT 3''')
    for resultTuple in result:
        print(resultTuple)

if KILL_DRILL_JVM:
    print('Killing Drill process...')
    killJvm('SqlLine')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53461078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档