使用下面的代码,p.returncode总是如此None.根据Popen.returncode文档,这意味着该过程尚未完成.
为什么我没有获得退出代码?
import os
import sys
import subprocess
cmd = ['echo','hello']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
# Filter stdout
for line in iter(p.stdout.readline, ''):
sys.stdout.flush()
# Print status
print(">>> " + line.rstrip())
sys.stdout.flush()
except:
sys.stdout.flush()
print 'RETURN CODE', p.returncode
Run Code Online (Sandbox Code Playgroud)
请注意:我之所以单独阅读每一行是因为我想实时过滤其他长期运行的进程的输出,并根据某些字符串暂停它们.
我使用的是Python 2.7.5(CentOS 7 64位).
感谢@skyking发布的答案,我现在可以使用Popen.poll()(Popen.wait()死锁我的进程)成功捕获这样的退出代码:
import os
import sys
import subprocess
import time
cmd = ['echo','hello']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
# Filter stdout
for line …Run Code Online (Sandbox Code Playgroud) 在继续执行之前等待postgres完全启动我的最佳方法是什么?ENTRYPOINTnosetests
现在我把我的机器上的启动时间定在50秒左右.所以我只是睡了60秒.这感觉不太好,因为在另一台机器上运行时可能无法正常工作.
ENTRYPOINT \
runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && \
sleep 60 && \
nosetests --verbose --cover-erase --with-coverage --cover-package=stalker
Run Code Online (Sandbox Code Playgroud)
这是启动的输出postgres:
2017-02-13 13:46:49.541 UTC [9] LOG: database system was interrupted; last known up at 2017-02-13 12:53:23 UTC
2017-02-13 13:47:37.951 UTC [9] LOG: database system was not properly shut down; automatic recovery in progress
2017-02-13 13:47:37.994 UTC [9] LOG: redo starts at 0/1783EA0
2017-02-13 13:47:37.995 UTC [9] LOG: record …Run Code Online (Sandbox Code Playgroud) 如果尚未安装,我该如何执行安装?
这是在OS X 10.9上,这是我目前所拥有的:
#!/bin/bash
# Local mount point
LOCALMOUNTPOINT="/folder/share"
# Perform the mount if it does not already exist
if ...
then
/sbin/mount -t smbfs //user:password@serveraddress/share $LOCALMOUNTPOINT
else
echo "Already mounted"
fi
Run Code Online (Sandbox Code Playgroud) 将条目插入/ etc/crontab的首选方法是什么,除非它存在,最好使用单行?
这是我希望放入/ etc/crontab的示例条目,除非它已存在于那里.
*/1 * * * * some_user python /mount/share/script.py
Run Code Online (Sandbox Code Playgroud)
我在CentOS 6.6上,到目前为止,我有这个:
if grep "*/1 * * * * some_user python /mount/share/script.py" /etc/crontab; then echo "Entry already in crontab"; else echo "*/1 * * * * some_user python /mount/share/script.py" >> /etc/crontab; fi
Run Code Online (Sandbox Code Playgroud) 使用Docker for Mac 1.12(稳定版)和OS X 10.11.5(15F34),我试图让我的一个容器转发到X11.
首先,我从我的bash shell安装XQuartz:
$ brew cask install xquartz
==> Downloading https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.9.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask xquartz
==> Running installer for xquartz; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
==> installer: Package name is XQuartz 2.7.9
==> installer: Installing at base path /
==> installer: The install was successful.
xquartz staged at '/usr/local/Caskroom/xquartz/2.7.9' (73M)
Run Code Online (Sandbox Code Playgroud)
然后我继续设置XQuartz ......
open -a …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我不能,pip install %CD%\*.whl因为我会得到:
Requirement 'C:\\Users\fredrik\\Downloads\\*.whl' looks like a filename, but the file does not exist
`*.whl is not a valid wheel filename.
Run Code Online (Sandbox Code Playgroud)
在macOS上(我相信Linux),我可以毫无问题地做到这一点:
pip install *.whl
Processing ./certifi-2017.11.5-py2.py3-none-any.whl
Processing ./chardet-3.0.4-py2.py3-none-any.whl
Processing ./idna-2.6-py2.py3-none-any.whl
Processing ./requests-2.18.4-py2.py3-none-any.whl
Processing ./urllib3-1.22-py2.py3-none-any.whl
...
Run Code Online (Sandbox Code Playgroud)
pip install *.whl)在Windows上工作?这个while循环如何限制为最多10次重试?
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
sleep 1
done
Run Code Online (Sandbox Code Playgroud) 我想创建一个不使用指向本地系统的符号链接的 virtualenv,因为我希望将 virtualenv(包括第三方包)与我的应用程序捆绑在一起。这是可能的 - 如果是,如何?
例如,在我的 Mac OS X 10.10.2 上,我创建的任何 virtualenv 都包含符号链接:
.Python -> /System/Library/Frameworks/Python.framework/Versions/2.7/Python
Run Code Online (Sandbox Code Playgroud)
如果我直接在网络服务器共享上创建我的 virtualenv,--always-copy我会收到一个错误:
$ virtualenv --always-copy python2.7.9_win7-64_stalker0.2.13
New python executable in python2.7.9_win7-64_stalker0.2.13/bin/python
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 11, in <module>
sys.exit(main())
File "/Library/Python/2.7/site-packages/virtualenv.py", line 825, in main
symlink=options.symlink)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 985, in create_environment
site_packages=site_packages, clear=clear, symlink=symlink))
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1374, in install_python
symlink)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 482, in copyfile
copyfileordir(src, dest, symlink)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 456, in copyfileordir
shutil.copy2(src, …Run Code Online (Sandbox Code Playgroud) 我在我的公共 Github 存储库的 Azure Pipeline 中成功创建了构建工件:
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: drop
Run Code Online (Sandbox Code Playgroud)
但是在 CI 构建之后,我想将这个工件下载到我的笔记本电脑并检查它。我所能找到的就是如何进行手动 REST API 调用,我需要知道我的内部版本号和工件名称。
是否无法下载从 Azure Pipelines Web 界面注册的工件?
我正在像这样点子安装我的模块:
cd my_working_dir
pip install -e .
Run Code Online (Sandbox Code Playgroud)
当我以后从Python导入模块时,是否可以某种方式检测模块是否以这种可编辑模式安装?
现在,我只是检查是否有一个.git文件夹os.path.dirname(mymodule.__file__)),只有在其中确实有一个.git文件夹时,它才起作用。有没有更可靠的方法?
bash ×4
python ×4
docker ×2
macos ×2
pip ×2
azure ×1
azure-devops ×1
centos6 ×1
postgresql ×1
virtualenv ×1
xquartz ×1