我正在使用eel创建一个桌面应用程序。但是,我同时运行函数时遇到了一个问题。在
eel的想法是允许electron通过JS与Python通信。因此,我在eelui中创建了一个按钮,它运行我程序的一部分(多个异步aiohttp会话)。运行这些函数的函数被定义为异步的,即async def main():。并且main()运行的所有函数也是异步的。以下是该计划的概要:import eel
import asyncio
from aiohttp import ClientSession
options = {
‘mode’: ‘custom’,
‘args’: [‘node_modules/electron/dist/electron.exe’, ‘.’],
}
@eel.expose
async def start_program(): # this is run when the user clicks the run button
await main()
@eel.expose
async def test_print(): # this is run when the user clicks the test button
print(“test”)
async def main():
tasks = []
loop = asyncio.get_event_loop()
for i in range(instances):
task = asyncio.ensure_future(launch(url))
tasks.append(task)
loop.run_until_complete(asyncio.wait(tasks))
async def launch(url):
done = False
async with ClientSession() as session:
while not done:
async with session.get(url, timeout=40) as initial:
html_text = await initial.text()
if x in html_text:
done = True
await session.close()
if __name__ == “__main__”:
eel.init(‘web’)
eel.start(‘html/index.html’, options=options)
然后在我的JavaScript中我有:
^{pr2}$
所以,期望的输出是,一旦我按下“运行”按钮,如果我再点击“测试”按钮,它将打印“测试”。然而,目前,两者都没有运行。我可以通过将async def main():改为def main():,async def start_program():改为{},并将{}改为{},这样就可以让“run”工作了。然后在JS里我改成了function start_program_js() { // this is run when the user clicks the run button
eel.start_program();
}
function test_print_js() { // this is run when the user clicks the test button
eel.test_print();
}
但是,现在我的问题是,如果我已经单击了“运行”,那么“测试”按钮将不能执行任何操作。它本身可以工作,但如果“run”正在运行,则不行。在
但我认为首先单击异步方法会导致错误Traceback (most recent call last):
File “src\gevent\greenlet.py”, line 766, in gevent._greenlet.Greenlet.run
File “C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eel\__init__.py”, line 209, in _process_message
‘value’: return_val }))
File “C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py”, line 231, in dumps
return _default_encoder.encode(obj)
File “C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py”, line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File “C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py”, line 257, in iterencode
return _iterencode(o, 0)
File “C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py”, line 179, in default
raise TypeError(f’Object of type {o.__class__.__name__} ‘
TypeError: Object of type coroutine is not JSON serializable
2019-06-03T13:00:59Z failed with TypeError
C:\Users\x\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gevent\libuv\loop.py:193: RuntimeWarning: coroutine ‘start_program’ was never awaited
super(loop, self)._run_callbacks()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
有人知道我怎么解决这个问题吗?在
谢谢。在
编辑:我刚刚注意到自述文件中有一个异步部分。我试着复制它,但是我不能让它工作。我试过了@eel.expose
def start_program(): # this is run when the user clicks the run button
eel.spawn(main)
@eel.expose
def test_print(): # this is run when the user clicks the test button
eel.spawn(test_print_x)
def test_print_x():
print(“test”)
但是,我遇到了和以前一样的问题,即它没有运行“测试”按钮功能。在