首页 > 解决方案 > Python子进程进程Popen在一段时间后暂停

问题描述

所以我有一些代码可以运行我的 Minecraft 服务器并使用子进程获取日志,但是在运行一段时间后,它会出现一些无法删除文件错误,服务器和程序继续运行,但不和谐机器人死了,它暂停收听过程。

@bot.event
async def on_ready():
    channel = bot.get_channel(895697946887200818)
    process = subprocess.Popen("start.bat", stdout=subprocess.PIPE)
    text = ""
    ready = False
    for c in iter(lambda: process.stdout.read(1), b''):  # replace '' with b'' for Python 3
        if not ready:
            if c.decode("utf-8") != "":
                if c.decode("utf-8") == "\n":
                    if text.strip() != "":
                        print(text)
                        if '[Server thread/INFO]: Done' in text.strip():
                            await channel.send("server is ready!")
                            ready = True
                        text = ""
                else:
                    text += c.decode("utf-8")
        else:
            if c.decode("utf-8") != "":
                if c.decode("utf-8") == "\n":
                    if text.strip() != "":
                        await channel.send(text)
                        text = ""
                else:
                    text += c.decode("utf-8")

这是错误

C:\Users\colin\OneDrive\Desktop\spigot 1.17>java -Xmx4096M -Xms2048M -jar spigot-1.17.1.jar nogui 
Loading libraries, please wait...
2021-10-07 18:57:58,810 main ERROR Unable to delete file C:\Users\colin\OneDrive\Desktop\spigot 1.17\logs\latest.log: java.nio.file.FileSystemException C:\Users\colin\OneDrive\Desktop\spigot 1.17\logs\latest.log: The process cannot access the file because it is being used by another process
[18:58:03] [main/INFO]: Environment: authHost='https://authserver.mojang.com', accountsHost='https://api.mojang.com', sessionHost='https://sessionserver.mojang.com', servicesHost='https://api.minecraftservices.com', name='PROD'
[18:58:04] [main/FATAL]: Failed to start the minecraft server
java.io.IOException: The process cannot access the file because another process has locked a portion of the file
    at sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[?:?]
    at sun.nio.ch.FileDispatcherImpl.write(FileDispatcherImpl.java:68) ~[?:?]
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:132) ~[?:?]
    at sun.nio.ch.IOUtil.write(IOUtil.java:76) ~[?:?]
    at sun.nio.ch.IOUtil.write(IOUtil.java:67) ~[?:?]
    at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:285) ~[?:?]
    at net.minecraft.util.SessionLock.a(SourceFile:40) ~[spigot-1.17.1.jar:3246-Spigot-6c1c1b2-dc764e7]
    at net.minecraft.world.level.storage.Convertable$ConversionSession.<init>(Convertable.java:341) ~[spigot-1.17.1.jar:3246-Spigot-6c1c1b2-dc764e7]
    at net.minecraft.world.level.storage.Convertable.c(Convertable.java:311) ~[spigot-1.17.1.jar:3246-Spigot-6c1c1b2-dc764e7]
    at net.minecraft.server.Main.main(Main.java:142) [spigot-1.17.1.jar:3246-Spigot-6c1c1b2-dc764e7]
    at org.bukkit.craftbukkit.Main.main(Main.java:206) [spigot-1.17.1.jar:3246-Spigot-6c1c1b2-dc764e7]
C:\Users\colin\OneDrive\Desktop\spigot 1.17>pause

此时它仍在运行,即使在最后一个错误行之后。C:\Users\colin\OneDrive\Desktop\spigot 1.17>暂停

标签: pythondiscord.pysubprocessminecraft

解决方案


推荐阅读