[英]How to use multiprocessing in eel
我的程序的邏輯如下:用戶點擊按鈕(在 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))
索引.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>
another_page.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');
}
另一個_page.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 上,但我想顯示,拜托
謝謝!!
請記住,當您使用多處理時,您將啟動一個具有單獨 memory 空間的單獨進程。 對象和變量沒有以任何方式連接。 如果需要通信,則需要使用Queue
或Pipe
。
通常,當您可以讓其他進程完成大量工作,然后通過隊列向母艦返回一些信號時,多處理效果最佳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.