Javascript 异步Python与eel

Javascript 异步Python与eel,javascript,python,asynchronous,async-await,eel,Javascript,Python,Asynchronous,Async Await,Eel,我正在使用创建一个桌面应用程序。但是,我在同时运行函数时遇到了一个问题 eel的思想是允许electron通过JS与Python通信。因此,我在eel UI中创建了一个按钮,它运行部分程序(多个异步aiohttp会话)。运行这些函数的函数定义为异步,即async def main():。而main()运行的所有函数也是异步的。以下是该计划的概要: 进口鳗鱼 导入异步 从aiohttp导入客户端会话 选项={ “模式”:“自定义”, 'args':['node_modules/electron/d

我正在使用创建一个桌面应用程序。但是,我在同时运行函数时遇到了一个问题

eel的思想是允许electron通过JS与Python通信。因此,我在eel UI中创建了一个按钮,它运行部分程序(多个异步aiohttp会话)。运行这些函数的函数定义为异步,即
async def main():
。而
main()
运行的所有函数也是异步的。以下是该计划的概要:

进口鳗鱼
导入异步
从aiohttp导入客户端会话
选项={
“模式”:“自定义”,
'args':['node_modules/electron/dist/electron.exe','.'],
}
@鳗鱼
async def start_program():#当用户单击“运行”按钮时运行此程序
等待主
@鳗鱼
异步def test_print():#当用户单击“测试”按钮时运行此操作
打印(“测试”)
异步def main():
任务=[]
loop=asyncio.get\u event\u loop()
对于范围内的i(实例):
任务=异步IO。确保未来(启动(url))
tasks.append(任务)
循环。运行_直到_完成(asyncio.wait(任务))
异步def启动(url):
完成=错误
与ClientSession()作为会话异步:
虽然没有这样做:
以session.get(url,timeout=40)为初始值的异步:
html_text=wait initial.text()
如果html_文本中有x:
完成=正确
等待会话。关闭()
如果名称=“\uuuuu main\uuuuuuuu”:
eel.init('web')
eel.start('html/index.html',options=options)
在我的JavaScript中,我有:

async function start_program_js() { // this is run when the user clicks the run button
    await eel.start_program()();
}

async function test_print_js() { // this is run when the user clicks the test button
    await eel.test_print()();
}

因此,所需的输出是,一旦我按下“运行”按钮,如果我单击“测试”按钮,它将打印“测试”。然而,目前两人都没有参选。通过将
async def main():
更改为
def main():
async def start_program():
更改为
def start_program():
async def test_print():
更改为
def test_print():
,我可以让“运行”正常。然后在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 <Greenlet at 0x4f4b4b0: _process_message({'call': 6.074835210306471, 'name': 'start_program', 'ar, <geventwebsocket.websocket.WebSocket object at 0x0)> 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")