简体   繁体   English

从 python 脚本运行 python 脚本但在 python 脚本之外

[英]Run python script from python script BUT outside of python script

It sounds like riddle or joke but actually I havent found answer to this problem.这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。

What is actually the problem?实际问题是什么?

I want to run 2 scripts.我想运行 2 个脚本。 In first script I call another script but I want them to continue parallely and not in 2 separate threads.在第一个脚本中,我调用了另一个脚本,但我希望它们并行运行,而不是在 2 个单独的线程中。 Mainly I dont want 2nd script to be running inside 1st python script(That means if I run Chrome Browser from python script and then shut down the python script, the Chrome will be shut down too).主要是我不希望第二个脚本在第一个 python 脚本中运行(这意味着如果我从 python 脚本运行 Chrome 浏览器然后关闭 python 脚本,Chrome 也将关闭)。

What I want is like on Linux machine: I open two terminals and run both scripts in each terminal - They are not two threads, they are independent on each other, shutting one will NOT shut down the other.我想要的是在 Linux 机器上:我打开两个终端并在每个终端中运行两个脚本 - 它们不是两个线程,它们相互独立,关闭一个不会关闭另一个。 Or it can be like on Linux machine where I can run 2 python scripts in terminal behind background with 'python xxx.py &' (&) symbol.或者它可以像在 Linux 机器上一样,我可以在后台的终端中运行 2 个 python 脚本,并带有 'python xxx.py &' (&) 符号。

Summary:概括:

I would like to run inside 'FIRST.py' script 'SECOND.py' script.我想在“FIRST.py”脚本“SECOND.py”脚本中运行。 However not with threading module and mainly have SECOND.py script independent on FIRST.py script, that is, shutting down FIRST.py will not have any consequence on SECOND.py.但是不带线程模块,主要是 SECOND.py 脚本独立于 FIRST.py 脚本,也就是说,关闭 FIRST.py 不会对 SECOND.py 产生任何影响。 THE SOLUTION SHOULD BE WORKING ON WINDOWS, LINUX AND MAC.该解决方案应该适用于 WINDOWS、LINUX 和 MAC。

BTW: I tried on windows:顺便说一句:我在 Windows 上试过:

subprocess.call(['python','second.py','&'])
subprocess.call(['python','second.py'])
os.system('python second.py') # I was desperate
  • They run serially, so first.py script is blocked untill second.py finishes.它们串行运行,因此 first.py 脚本被阻止,直到 second.py 完成。

I havent try Threading with daemon=False but I feel its kind of Demon and I dont feel my skill is that far that I can control threads existing outside of my playground :)我还没有尝试使用 daemon=False 进行线程处理,但我觉得它是一种恶魔,而且我觉得我的技能还不够到我可以控制存在于我的操场之外的线程的程度:)

Thanks in advance for help预先感谢您的帮助

You can use the Popen constructor from the subprocess module to launch background processes, using您可以使用Popen构造从subprocess模块启动后台进程,使用

import subprocess
p = subprocess.Popen(["python","second.py"])

creates a background process and execution of first.py is not blocked.创建一个后台进程并且first.py执行不会被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM