我的程序逻辑如下: 用户单击按钮(在UI上)
->;多处理计数器的触发方法(从js到python)
->;启动多处理计数器循环(在python中)
->;向UI显示计数器编号(通过js从python到html)
我的问题是为什么它不能在UI上显示计数器编号
以下是我的代码:
python:
import eel,time
from multiprocessing import Process
count = 0
eel.init('static')
@eel.expose
def do_something():
global p
p = Process(target=run_forever)
p.start()
print('start a process.')
print(f'The process is alive or not:{p.is_alive}')
def run_forever():
global count
while 1:
print(time.time())
time.sleep(1)
count = count + 1
eel.js_counter(count)
@eel.expose
def end_do_something():
global p
p.terminate()
p.terminate()
print('stop process')
p.join()
print(f'The process is alive or not:{p.is_alive}')
if __name__ == '__main__':
eel.start('index.html',size=(600,400))
index.html:
<h1>Hello World</h1>
<p ></p>
<b></b>
<form>
<input type="text" id="myText" required/>
<input type="submit" onclick="return input_val()" value="print text">
</form>
<button class="call_python" onclick="btn_index()">Call_python start timing</button>
<a href="another_page.html">go to another page</a>
<script type="text/javascript" src="./index.js"></script>
<script type="text/javascript" src="/eel.js"></script>
另一个页面。html:
<h1>Hello World</h1>
<p>This is home</p>
<input type="button" onclick="change_input_val()" value="change text">
<input type="button" onclick="end_counter()" value="end counter">
<a href="index.html">go back to index</a>
<script type="text/javascript" src="./another_page.js"></script>
<script type="text/javascript" src="/eel.js"></script>
index.js:
async function btn_index(){
await eel.do_something()();
}
eel.expose(js_counter);
function js_counter(count){
sessionStorage.setItem("counter", count);
document.querySelector('p').innerHTML = count
}
function input_val(){
document.querySelector('b').innerHTML = document.getElementById("myText").value;
sessionStorage.setItem("test", document.getElementById("myText").value);
return false;
}
window.onload = function (){
document.querySelector('b').innerHTML = sessionStorage.getItem('test');
document.querySelector('p').innerHTML = sessionStorage.getItem('counter');
}
另一个页面。js:
function change_input_val(){
let a = sessionStorage.getItem('test');
a += 'testing';
sessionStorage.setItem("test", a);
}
async function end_counter(){
await eel.end_do_something()();
}
环境:
问题:
如果我阻止多处理程序,它会工作!!(显示计数器的编号)
但我想用多重处理来调用计数器
有人能帮我解决这个问题吗,不要在UI上显示,但我想显示
谢谢
请记住,当您使用多处理时,您正在启动一个具有单独内存空间的单独进程。对象和变量之间没有任何连接。如果需要通信,则需要使用
Queue
或Pipe
一般来说,当您可以让另一个进程做大量工作,然后通过队列向母舰返回一些信号时,多处理效果最佳
相关问题 更多 >
编程相关推荐