我需要在我的 Django 应用程序中使用一个 HTML 网页来加载并在可滚动框中显示脚本的连续输出。这可能吗?
我目前正在使用子进程来运行 Python 脚本,但 HTML 页面在脚本完成后才会加载(这可能需要大约 5 分钟)。我希望用户看到正在发生的事情,而不仅仅是一个旋转的圆圈。
我已经用文本中的“\n”卸载了脚本的完整输出;如果可能,我希望它输出每个新行。
我的代码如下:
视图.py:
def projectprogress(request):
GenerateProjectConfig(request)
home = os.getcwd()
project_id = request.session['projectname']
staging_folder = home + "/staging/" + project_id + "/"
output = ""
os.chdir(staging_folder)
script = home + '/webscripts/terraformdeploy.py'
try:
output = subprocess.check_output(['python', script], shell=True)
except subprocess.CalledProcessError:
exit_code, error_msg = output.returncode, output.output
os.chdir(home)
return render(request, 'projectprogress.html', locals())
Run Code Online (Sandbox Code Playgroud)
项目进度.html:
<style>
div.ex1 {
background-color: black;
width: 900px;
height: 500px;
overflow: scroll;
margin: 50px;
}
</style> …Run Code Online (Sandbox Code Playgroud) 关于本规范:http : //www.w3.org/TR/eventsource/
如何关闭服务器上打开的连接?客户端明智很容易,只需调用close(),但是我应该在服务器上做什么?就杀了?
我正在尝试使用 Spring SseEmitter实现服务器发送事件,如此Youtube 视频中所述。
我能够启动事件流并从服务器发送的事件接收数据。
但是,我可以看到EventStream从客户端发出并到达服务器的多个类型的请求。我理解它的方式,EventSource应该发送一个HTTP请求,然后应该保持half duplex与服务器的连接,使用哪个服务器将事件发送到客户端。
为什么它会定期发送请求?是不是就像轮询而不是半双工连接?
波纹管是我正在使用的代码。
服务器代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder;
@RestController
public class TestService {
private List<SseEmitter> subscriberList = Collections.synchronizedList(new ArrayList<>());
@RequestMapping("inbox")
public SseEmitter inbox() {
SseEmitter subscriber = new SseEmitter();
subscriberList.add(subscriber);
subscriber.onCompletion(() -> {
subscriberList.remove(subscriber);
System.out.println("Removed the completed event emitter");
});
System.out.println("Subscriber arrived");
return subscriber;
}
@RequestMapping("message")
public String message(@RequestParam("message") …Run Code Online (Sandbox Code Playgroud)