V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
WinG
V2EX  ›  Python

Python 怎么同时打开几次同一个.py 文件

  •  
  •   WinG · 2019-02-27 10:58:48 +08:00 · 2517 次点击
    这是一个创建于 1878 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我用的是 Windows 系统,有两个.py 文件

    a.py
    b.py

    a.py 文件是一个循环:

    for i in [1,2,3]:
    执行 b.py 1
    b.py 2
    b.py 3
    怎么同时打几次同一个.py 文件 传入不同参数。要新的 cmd 窗口的。

    我查了试了 2 个方法 在 a.py 里写:
    os.system("python b.py")
    subprocess.call('start/wait python b.py', shell=True)

    但是要么是在同一个 cmd 内运行,要么是新打开一个 cmd,但是下一个循环就卡住了,必须关掉一个,第二个循环才把新窗口弹出来。

    我需要 b.py 1,b.py 2,b.py 3 三个同时打开独立进程新 cmd 窗口,不用返回什么值。

    试了很多次,所以请教大家。
    Trim21
        1
    Trim21  
       2019-02-27 11:04:49 +08:00 via Android   ❤️ 1
    为什么不包成一个函数多次调用呢…
    clino
        2
    clino  
       2019-02-27 11:08:45 +08:00   ❤️ 1
    此时应该用 subprocess.Popen() ,如果要等就主动调 process object 的 wait() 方法
    我想 subprocess.call() 里面应该调用了 wait()
    CallMeReznov
        3
    CallMeReznov  
       2019-02-27 11:12:29 +08:00   ❤️ 1
    批处理命令 start 了解一下?
    SeaRecluse
        4
    SeaRecluse  
       2019-02-27 11:12:36 +08:00   ❤️ 1
    没看懂,重新组织下语言。你为什么需要开新的 cmd 窗口?

    a.py
    ```python
    import subprocess
    from subprocess import Popen, PIPE, STDOUT

    if __name__ == '__main__':
    for i in range(1,4):
    cmd = "python b.py " + str(i)
    with Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT) as res:
    out = res.stdout.read()
    out = bytes.decode(out,encoding = "utf-8")
    print(out)

    ```

    b.py
    ```python
    import sys

    if __name__ == '__main__':
    print(sys.argv[1])
    ```
    reself
        5
    reself  
       2019-02-27 11:16:24 +08:00   ❤️ 1
    windows 文件占用问题。换 linux 可解。
    zwh2698
        6
    zwh2698  
       2019-02-27 11:30:41 +08:00 via Android   ❤️ 3
    办法 1. 将 system 包在线程中,随便循环
    办法 2. 使用启动进程 API, python 的或者 os 的都行,不要 wait.

    进程相关知识你还不是很熟悉,另外楼上有同学回复有误,不要被误导
    bakabie
        7
    bakabie  
       2019-02-27 11:44:43 +08:00 via Android   ❤️ 1
    楼上+1,顺带还有一种解决方法是 os.system("cmd /c python x.py")
    smdbh
        8
    smdbh  
       2019-02-27 12:57:18 +08:00   ❤️ 1
    mythmgn
        9
    mythmgn  
       2019-03-05 17:19:26 +08:00   ❤️ 1
    1. 简单写着玩就用 threading 就可以

    2. 如果想正经写, 可以考虑线程池.

    cup 的线程池满足你要求:

    https://github.com/baidu/CUP/blob/master/cup/services/threadpool.py

    3. 或者用异步 shell
    cup.shell.oper 中的 ShellExec

    看你的选择吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3630 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:28 · PVG 18:28 · LAX 03:28 · JFK 06:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.