Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 例外情况;ConnectionResetError“;等待asyncio.subprocess模块中的subprocess.stdout.drain()之后_Python_Python 3.x_Python Asyncio - Fatal编程技术网

Python 例外情况;ConnectionResetError“;等待asyncio.subprocess模块中的subprocess.stdout.drain()之后

Python 例外情况;ConnectionResetError“;等待asyncio.subprocess模块中的subprocess.stdout.drain()之后,python,python-3.x,python-asyncio,Python,Python 3.x,Python Asyncio,我在尝试使用asyncio.subprocess运行外部python文件时遇到了一个异常。这是我的密码: import asyncio async def external(): writing_process=await asyncio.create_subprocess_shell("pwd", stdin=asyncio.subprocess.PIPE,

我在尝试使用asyncio.subprocess运行外部python文件时遇到了一个异常。这是我的密码:

import asyncio
async def external():
    writing_process=await asyncio.create_subprocess_shell("pwd",
                                                            stdin=asyncio.subprocess.PIPE, 
                                                            stdout=asyncio.subprocess.PIPE, 
                                                            stderr=asyncio.subprocess.PIPE,
                                                            cwd="/home/user1/folder1")
    print(await writing_process.stdout.read())
    writing_process.stdin.writelines([b"python test.py"])
    await writing_process.stdin.drain()
    print(await writing_process.stdout.read())
asyncio.run(run_outer_py_file())
以下是输出:

b'/home/bykov/nra_banks\n'
---------------------------------------------------------------------------
ConnectionResetError                      Traceback (most recent call last)
<ipython-input-9-67bbc190049c> in async-def-wrapper()

<ipython-input-9-67bbc190049c> in external()
     11     print(await writing_process.stdout.read())
     12 await external()

~/anaconda3/lib/python3.7/asyncio/streams.py in drain(self)
    346             # would not see an error when the socket is closed.
    347             await sleep(0, loop=self._loop)
--> 348         await self._protocol._drain_helper()
    349 
    350 

~/anaconda3/lib/python3.7/asyncio/streams.py in _drain_helper(self)
    200     async def _drain_helper(self):
    201         if self._connection_lost:
--> 202             raise ConnectionResetError('Connection lost')
    203         if not self._paused:
    204             return

ConnectionResetError: Connection lost
b'/home/bykov/nra\u banks\n'
---------------------------------------------------------------------------
ConnectionResetError回溯(最近一次调用上次)
在async-def-wrapper()中
在外部()
11打印(等待写入进程.stdout.read())
12等待外部
排水管中的~/anaconda3/lib/python3.7/asyncio/streams.py(自)
346#在套接字关闭时不会看到错误。
347等待睡眠(0,循环=自循环)
-->348等待自我。_协议。_排出_帮助者()
349
350
~/anaconda3/lib/python3.7/asyncio/streams.py in\u drain\u helper(self)
200异步def_排出辅助程序(自身):
201如果自身连接丢失:
-->202 raise CONNECTIONRESET错误(“连接丢失”)
203如果不是自己。\u暂停:
204返回
ConnectionResetError:连接丢失

有谁能解释一下,我错在哪里?

根据@user4815162342,我知道我应该将所有命令放在bash脚本中,并使用python文件的名称作为命令行参数

因此,我的bash脚本:

#!/bin/bash
source /home/user1/anaconda3/bin/activate my_env
python "$@"
还有我的python代码:

import asyncio
async def run_external(script, *args):
    writing_process=await asyncio.create_subprocess_shell("bash test.sh "+' '.join((script,)+args), 
                                                      stdin=asyncio.subprocess.PIPE, 
                                                      stdout=asyncio.subprocess.PIPE, 
                                                      stderr=asyncio.subprocess.PIPE,
                                                      cwd="/home/user1/folder1")
    print((await writing_process.stdout.read()).decode())
asyncio.run(run_external("test.py"))

请尝试创建一个最小的可运行示例,其他人可以运行该示例来重现问题。我刚刚根据@user4815162342编辑了我的注释,这里的问题是
pwd
很快退出,写入失败,出现异常,异常由
drain()报告。您需要确保子流程处于活动状态,或者捕获
IOError
。可能同样的问题也适用于您的原始代码,其中子进程由于某些错误而退出。@user4815162342谢谢。据我所知,我可以将所有命令作为第一个粗略的解决方案放在bash文件中;例如,您的代码无法正确地将包含空格的参数传递给子流程。您可以改为使用
create_subprocess_exec(“bash”、“test.sh”、script、*args、stdin=…)