I am creating a desktop app with the use of eel. However, I am having an issue with running functions at the same time.
The idea of eel is that it allows electron to communicate with Python via JS. So, I have created a button in the eel UI that runs part of my program (multiple asynchronous aiohttp sessions). The function that runs those is defined as asynchronous, i.e. async def main():
. And all of the functions run by main()
are also async. Here is a general outline of the program:
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)
And then in my JavaScript I have:
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()();
}
So, the desired output is that once I have already pressed the "run" button, if I then click the "test" button, it will print "test". However, at the moment, neither are running. I could get "run" to work by changing async def main():
to def main():
, async def start_program():
to def start_program():
and async def test_print():
to def test_print():
. Then in the JS I changed it to
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();
}
However, now my issue is that the "test" button will not do anything if I have already clicked "run". It works by itself, but not if "run" is running.
I thought the async approach I detailed first would work, but using that and clicking "run" caused this error:
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
Does anyone know how I can resolve this issue?
Thanks.
EDIT: I just noticed there is an asynchronous section in the readme. I have tried to replicate it, but I can't get it working. I tried
@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")
However, I'm having the same issue as before, i.e. it is not running the "test" button function.