相关疑难解决方法(0)

主脚本停止时后台进程的参数错误无效

我有这个代码通过运行后台进程来获取推文.以下脚本使用subprocess.Popen函数从主脚本运行.这样主脚本将在调用后台进程脚本后停止执行.

def start_listner(unique_id, keyword, limit=200):
    class CustomStreamListener(tweepy.StreamListener):

        def __init__(self, api):
            logger.info('runnning')
            self.api = api
            super(tweepy.StreamListener, self).__init__()

            #setup rabbitMQ Connection


        def on_status(self, status):
            print status.text.encode('utf-8'), "\n"
            #queue the tweet and writes the tweet to the log

        def on_error(self, status_code):
          #some code to not kill the stream

        def on_timeout(self):
          #some code to not kill the stream

    sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
    try:
        logger.info('tracking started')
        logger.info(keyword)
        logger.info(type(keyword))
        kw = keyword
        sapi.filter(track=[kw])  # keeps listening to the streaming api
    except Exception, err:
        logger.info(kw) …
Run Code Online (Sandbox Code Playgroud)

python daemon background-process tweepy python-2.7

10
推荐指数
1
解决办法
565
查看次数

在Python中运行后台进程,不要等待

我的目标很简单:启动rsync并且不要等待.

Debian上的Python 2.7.9

示例代码:

rsync_cmd = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1)
rsync_cmd2 = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3} &".format(remote_user, remote_server, file1, file1)
rsync_path = "/usr/bin/rsync"
rsync_args = shlex.split("-a -e 'ssh -i /home/mysuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1))
#subprocess.call(rsync_cmd, shell=True)     # This isn't supposed to work but I tried it
#subprocess.Popen(rsync_cmd, shell=True)    # This is supposed to be the solution but not for me
#subprocess.Popen(rsync_cmd2, shell=True)   # Adding my own shell "&" …
Run Code Online (Sandbox Code Playgroud)

python subprocess popen background-process

10
推荐指数
2
解决办法
4万
查看次数

使用python启动一个独立的进程

这真是一个非常简单的问题,但我似乎无法找到任何解决方案.

我有一个python脚本,我想启动一个独立的守护进程.我想调用ym python脚本,启动这个系统托盘dameon,在数据库文件上做一些python魔法并退出,让系统托盘守护进程运行.

我试过os.system,subprocess.call,subprocess.Popen,os.execl,但它始终保持我的剧本还活着,直到我关闭系统托盘守护进程.

这听起来应该是一个简单的解决方案,但我无法得到任何工作.

编辑:Windows解决方案:http:os.startfile() //docs.python.org/library/os.html?highlight = startfile#os.startfile

有时放弃和询问意味着你只是在答案的尖端.

python windows subprocess daemon process

9
推荐指数
2
解决办法
1万
查看次数

如何在后台python中运行子进程

我正在python中编写一个应用程序,它通过使用python子进程调用shell脚本来启动java中的JVM.但是,我的问题是我编写的正确方法,JVM启动并阻止其后发生的其余进程.我需要JVM在我调用另一个函数时运行,并且我需要在进程运行完毕后停止JVM.

Python代码:

process = subprocess.Popen('runJVM.sh', shell=True, stderr=subprocess.STDOUT)
process.wait()

r = Recommender()
r.load()
assert len(sys.argv) > 1, '%d arguments supplied, one needed' %(len(sys.argv)-1)
print "recommendations" + str(r.get_users_recommendation(sys.argv[1:]))

....
def get_users_recommendation(self, user_list):
    j_id_list = ListConverter().convert(class_list, self.gateway._gateway_client)
    recs = self.gateway.entry_point.recommend(j_id_list)
    return recs
Run Code Online (Sandbox Code Playgroud)

哪里:

 from py4j.java_gateway import JavaGateway
 self.gateway = JavaGateway()
Run Code Online (Sandbox Code Playgroud)

我无法get_user_recommendations运行,因为JVM服务器阻止了该进程.我如何不阻止Python脚本的其余部分,然后在python方法运行完毕并且返回值后终止它?非常感谢.

python subprocess

8
推荐指数
1
解决办法
2万
查看次数

Python:非阻塞+非解散过程

我想创建一个父进程,它将创建许多子进程.由于父进程负责创建子进程,因此父进程不关心子进程的状态.

由于subprocess.call是阻塞的,它不起作用.因此我使用subprocess.Popen来替换call.然而,一旦孩子终止(链接),Popen将生成僵尸(已解散)进程.

有没有办法解决这个问题?

提前致谢

python subprocess

7
推荐指数
1
解决办法
8687
查看次数

从python脚本中启动python脚本作为后台进程

我的python脚本需要启动后台进程,然后继续处理完成而不等待返回.

后台脚本将处理一段时间,不会生成任何屏幕输出.

不需要进程间数据.

我尝试过使用各种方法子进程,多处理,但显然缺少一些东西.

有没有人有一个简单的例子?

TIA

python

5
推荐指数
1
解决办法
6278
查看次数

如何启动进程并将其放入python的后台?

我目前正在编写我的第一个python程序(在Python 2.6.6中).该程序有助于启动和停止在服务器上运行的不同应用程序,这些应用程序提供用户公共命令(例如在Linux服务器上启动和停止系统服务).

我正在启动应用程序的启动脚本

p = subprocess.Popen(startCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
print(output)
Run Code Online (Sandbox Code Playgroud)

问题是,一个应用程序的启动脚本保持在前台,因此p.communicate()会永远等待.我已经尝试在startCommand前使用"nohup startCommand&",但是没有按预期工作.

作为一种解决方法,我现在使用以下bash脚本来调用应用程序的启动脚本:

#!/bin/bash

LOGFILE="/opt/scripts/bin/logs/SomeServerApplicationStart.log"

nohup /opt/someDir/startSomeServerApplication.sh >${LOGFILE} 2>&1 &

STARTUPOK=$(tail -1 ${LOGFILE} | grep "Server started in RUNNING mode" | wc -l)
COUNTER=0

while [ $STARTUPOK -ne 1 ] && [ $COUNTER -lt 100 ]; do
   STARTUPOK=$(tail -1 logs/SomeServerApplicationStart.log | grep "Server started in RUNNING mode" | wc -l)
   if (( STARTUPOK )); then
      echo "STARTUP OK"
      exit 0
   fi
   sleep 1
   COUNTER=$(( $COUNTER + …
Run Code Online (Sandbox Code Playgroud)

python linux bash subprocess daemon

5
推荐指数
1
解决办法
1145
查看次数

在Python数据流/ Apache Beam上启动CloudSQL代理

我目前正在从事ETL Dataflow作业(使用Apache Beam Python SDK),该作业从CloudSQL查询数据(带有psycopg2和自定义ParDo)并将其写入BigQuery。我的目标是创建一个数据流模板,该模板可以使用Cron作业从AppEngine开始。

我有一个使用DirectRunner在本地工作的版本。为此,我使用CloudSQL(Postgres)代理客户端,以便可以连接到127.0.0.1上的数据库。

当将DataflowRunner与自定义命令一起使用来在setup.py脚本中启动代理时,该作业将不会执行。它坚持重复此日志消息:

Setting node annotation to enable volume controller attach/detach

我的setup.py的一部分看起来如下:

CUSTOM_COMMANDS = [
['echo', 'Custom command worked!'],
['wget', 'https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64', '-O', 'cloud_sql_proxy'],
['echo', 'Proxy downloaded'],
['chmod', '+x', 'cloud_sql_proxy']]

class CustomCommands(setuptools.Command):
  """A setuptools Command class able to run arbitrary commands."""

  def initialize_options(self):
    pass

  def finalize_options(self):
    pass

  def RunCustomCommand(self, command_list):
    print('Running command: %s' % command_list)
    logging.info("Running custom commands")
    p = subprocess.Popen(
        command_list,
        stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    # Can use communicate(input='y\n'.encode()) if the command run …
Run Code Online (Sandbox Code Playgroud)

python google-cloud-sql google-cloud-dataflow apache-beam cloud-sql-proxy

5
推荐指数
2
解决办法
1336
查看次数

将参数传递给 python eval()

我正在做遗传编程框架,我需要能够执行一些代表完整Python程序的字符串。我正在使用Python 2.7。我有一个配置类,其中定义了原始集。可以说

class Foo():
    def a(self,x):
        return x

    def b(self,y):
        return y
Run Code Online (Sandbox Code Playgroud)

我正在使用 python 检查模块提取函数,并且我想创建一些包含导入和所有内容的可执行源代码。我最终得到了一个看起来像这样的字符串

import sys

def a(x,y):
    return x

def b(y):
    return y

def main(x,y)
    lambda x,y: a(b(y),a(x,y))

main(*sys.argv)
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何将命令行参数传递给我正在运行的字符串eval()如何将命令行参数传递给我想要运行的源文件eval()

编辑:有数百万人,因此写入文件并不是一个好的选择。

编辑:我犯了一个错误。eval() 方法仅用于表达式而不是语句,因此使用 exec() 是正确的方法

python code-generation eval argument-passing

3
推荐指数
1
解决办法
1万
查看次数