如何退出调用 Django 服务器的子进程?
How can I exit a subprocess that calls a django server?
我正在 运行在 localhost:8000 上的 django 服务器上运行一个 otree erperiment,我想 运行 一个在 8080 上启动另一个 django 服务器的子进程。
如果首先调用子流程,则浏览器选项卡将不会打开。我怎样才能解决这个问题?
我怎样才能退出新服务器(和浏览器选项卡)并返回我的第一个服务器?
这是一个 oTree 实验。在这个实验中,我需要打开另一个django项目,特别是我想运行一个django-oscar shop。
class MyPage2(Page):
def before_next_page(self):
os.environ['DJANGO_SETTINGS_MODULE'] = 'shop.settings'
subprocess.call(['py', 'shop2/manage.py', 'runserver', '0.0.0.0:8080'], shell=True)
webbrowser.open('http://localhost:8080', new=2)
我不确定你想要什么,但如果你想终止一个子进程,只需调用 os.killpg
如下:
尝试下面的代码,将 call
替换为 Popen
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
解决这个问题的方式有点不同。
我已经放弃了子进程,而是用 django REST 框架创建了一个 API。来回传递参数(并在同一个浏览器选项卡中工作)是最好的方法。
我正在 运行在 localhost:8000 上的 django 服务器上运行一个 otree erperiment,我想 运行 一个在 8080 上启动另一个 django 服务器的子进程。 如果首先调用子流程,则浏览器选项卡将不会打开。我怎样才能解决这个问题? 我怎样才能退出新服务器(和浏览器选项卡)并返回我的第一个服务器?
这是一个 oTree 实验。在这个实验中,我需要打开另一个django项目,特别是我想运行一个django-oscar shop。
class MyPage2(Page):
def before_next_page(self):
os.environ['DJANGO_SETTINGS_MODULE'] = 'shop.settings'
subprocess.call(['py', 'shop2/manage.py', 'runserver', '0.0.0.0:8080'], shell=True)
webbrowser.open('http://localhost:8080', new=2)
我不确定你想要什么,但如果你想终止一个子进程,只需调用 os.killpg
如下:
尝试下面的代码,将 call
替换为 Popen
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
解决这个问题的方式有点不同。 我已经放弃了子进程,而是用 django REST 框架创建了一个 API。来回传递参数(并在同一个浏览器选项卡中工作)是最好的方法。