【python 执行系统命令---suprocess模块】教程文章相关的互联网学习教程文章

为什么concurrent.futures.ProcessPoolExecutor和multiprocessing.pool.Pool在Python中使用super失败?【代码】

为什么使用concurrent.futures模块的以下Python代码永远挂起?import concurrent.futuresclass A:def f(self):print("called")class B(A):def f(self):executor = concurrent.futures.ProcessPoolExecutor(max_workers=2)executor.submit(super().f)if __name__ == "__main__":B().f()该调用引发了一个不可见的异常[Errno 24]太多打开的文件(要查看它,用print执行器替换行executor.submit(super().f)(executor.submit(super().f).e...

如何将具有多个参数的函数传递给python concurrent.futures.ProcessPoolExecutor.map()?【代码】

我想concurrent.futures.ProcessPoolExecutor.map()来调用一个由2个或更多参数组成的函数.在下面的示例中,我使用了lambda函数并将ref定义为具有相同值的numberlist大小相等的数组. 第一个问题:有更好的方法吗?在numberlist的大小可能是数百万到数十亿个元素的情况下,因此ref大小必须遵循numberlist,这种方法不必要地占用宝贵的内存,我想避免.我这样做是因为我读取map函数将终止其映射,直到达到最短的数组结束.import concurrent....

python – easyprocess.EasyProcessCheckInstalledError:cmd = [‘Xvfb’,’ – help’] OSError = [Errno 2]没【代码】

我正在尝试设置显示器,但它告诉我没有这样的文件或目录.Traceback (most recent call last):File "/var/www/envs/venv/proj/scripts/my_file.py", line 44, in clickdisplay = Display(visible=0, size=(800,600))File "/usr/lib/python2.6/site-packages/pyvirtualdisplay/display.py", line 33, in __init__self._obj = self.display_class(File "/usr/lib/python2.6/site-packages/pyvirtualdisplay/display.py", line 51, in d...

使用python和subprocess,Pipe,Popen从hdfs读取/写入文件会出错【代码】

我试图在python脚本中读取(打开)和写入hdfs中的文件.但有错误.谁能告诉我这里有什么问题. 代码(完整):sample.py#!/usr/bin/pythonfrom subprocess import Popen, PIPEprint "Before Loop"cat = Popen(["hadoop", "fs", "-cat", "./sample.txt"],stdout=PIPE)print "After Loop 1" put = Popen(["hadoop", "fs", "-put", "-", "./modifiedfile.txt"],stdin=PIPE)print "After Loop 2" for line in cat.stdout:line += "Blah"print ...

Python的subprocess.call和subprocess.run之间有什么区别

我一直试图理解一下,现在subprocess.call和subprocess.run之间的区别是什么.我知道最后一个是Python 3.5上的新内容,两者都基于subprocess.Popen,但我还不能理解其中的差异.解决方法:subprocess.call()的定义明确提到:It is equivalent to: run(...).returncode(except that the input and check parameters are not supported)正如Python 3.5’s subprocess document所说:Prior to Python 3.5, these three functions (i.e. .cal...

python – 所有示例concurrent.futures代码都失败了“BrokenProcessPool”【代码】

在创建我需要的实际应用程序之前,我试图对此有一个基本的了解.我最近从2.7移到了3.3. this code from the python docs的直接复制粘贴失败,here的稍微简单的示例也失败了. 这是我的代码,派生自第二个例子:import concurrent.futuresnums = [1,2,3,4,5,6,7,8,9,10]def f(x):return x * x# Make sure the map and function are working print([val for val in map(f, nums)])# Test to make sure concurrent map is working with con...

python – subprocess.popen和subprocess.run之间的区别是什么

我是子进程模块的新手,而且这个问题让我想知道subprocess.popen和subprocess.run之间有什么区别.这个命令有什么不同吗?一个人刚刚更新吗?哪个更好用?解决方法:subprocess.run was added in Python 3.5作为subprocess.Popen的简化,当你只想执行命令并等到它完成时,但你不想同时做任何其他事情.对于其他情况,您仍然需要使用subprocess.Popen. 主要区别在于subprocess.run执行命令并等待它完成,而使用subprocess.Popen,您可以在进程...

Python multiprocessing.Queue vs multiprocessing.manager().Queue()【代码】

我有一个简单的任务:def worker(queue):while True:try:_ = queue.get_nowait()except Queue.Empty:breakif __name__ == '__main__':manager = multiprocessing.Manager()# queue = multiprocessing.Queue()queue = manager.Queue()for i in range(5):queue.put(i)processes = []for i in range(2):proc = multiprocessing.Process(target=worker, args=(queue,))processes.append(proc)proc.start()for proc in processes:proc.j...

python – 将变量传递给Subprocess.Popen【代码】

我有一个脚本,它通过subprocess.Popen模块调用另一个python脚本.但因为我有变量存储的参数servers[server]['address'] servers[server]['port'] servers[server]['pass']我无法执行命令p = subprocess.Popen(["python mytool.py -a ", servers[server]['address'], "-x", servers[server]['port'], "-p", servers[server]['pass'], "some additional command"], shell=True, stdout=subprocess.PIPE)解决方法:Drop shell = True. T...

python concurrent.futures.ProcessPoolExecutor:.submit()vs .map()的性能【代码】

我使用concurrent.futures.ProcessPoolExecutor来查找数字范围内的数字的出现.目的是调查从并发中获得的加速性能的数量.为了测试性能,我有一个控件 – 一个执行所述任务的串行代码(如下所示).我编写了2个并发代码,一个使用concurrent.futures.ProcessPoolExecutor.submit(),另一个使用concurrent.futures.ProcessPoolExecutor.map()执行相同的任务.它们如下所示.起草前者和后者的建议分别可以在here和here看到. 发给所有三个代码的...

Python subprocess.Popen()错误(没有这样的文件或目录)【代码】

我试图使用Python函数计算文件中的行数.在当前目录中,当os.system(“ls”)找到该文件时,命令subprocess.Popen([“wc -l filename”],stdout = subprocess.PIPE)不起作用. 这是我的代码:>>> import os >>> import subprocess >>> os.system("ls") sorted_list.dat 0 >>> p = subprocess.Popen(["wc -l sorted_list.dat"], stdout=subprocess.PIPE)File "<stdin>", line 1, in <module> File "/Users/a200/anaconda/lib/python2.7/s...

python – 为什么subprocess.Popen参数长度限制小于操作系统报告的限制?【代码】

我在Linux 3.16.0上运行Python 3.4.3.我想使用subprocess.Popen运行一个带有长单个参数(复杂的Bash调用)的命令,大约200KiB. 根据getconf和xargs,这应该在我的范围内:$getconf ARG_MAX 2097152 $xargs --show-limits < /dev/null Your environment variables take up 3364 bytes POSIX upper limit on argument length (this system): 2091740 POSIX smallest allowable upper limit on argument length (all systems): 4096 Maxim...

如何在Python中使用multiprocessing.pool创建全局锁/信号量?【代码】

我想在子进程中限制资源访问.例如 – 限制http下载,磁盘io等.我如何实现它扩展这个基本代码? 请分享一些基本的代码示例.pool = multiprocessing.Pool(multiprocessing.cpu_count()) while job_queue.is_jobs_for_processing():for job in job_queue.pull_jobs_for_processing:pool.apply_async(do_job, callback = callback) pool.close() pool.join()解决方法:创建池时使用initializer和initargs参数,以便在所有子进程中定义全局...

python – 使用subprocess.Popen通过SSH或SCP发送密码【代码】

我正在尝试使用subprocess.Popen运行scp(安全复制)命令.登录要求我发送密码:from subprocess import Popen, PIPEproc = Popen(['scp', "user@10.0.1.12:/foo/bar/somefile.txt", "."], stdin = PIPE) proc.stdin.write(b'mypassword') proc.stdin.flush()这会立即返回错误:user@10.0.1.12's password: Permission denied, please try again.我确定密码是正确的.我通过在shell上手动调用scp轻松验证它.那么为什么这不起作用呢? 注...

Python subprocess模块

一、subprocess以及常用的封装函数运行python的时候,我们都是在创建并运行一个进程。像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序。subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(standa...

模块 - 相关标签