Skip to content

Replace use of Thread with ThreadPoolExecutor #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ install:
# compiled extensions and are not provided as pre-built wheel packages,
# pip will build them from source using the MSVC compiler matching the
# target Python version and architecture
- "%CMD_IN_ENV% pip install pytest==4.2.0 pythonnet six cefpython3==66.0"
- "%CMD_IN_ENV% pip install \"pytest>=4.3.0\" pythonnet six cefpython3==66.0"


test_script:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'pythonnet ; sys_platform == "win32"',
'pyobjc ; sys_platform == "darwin"',
'PyQt5 ; sys_platform == "openbsd6"',
'futures; python_version < "3.2"',
]


Expand Down
22 changes: 11 additions & 11 deletions webview/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import re
import sys
from concurrent.futures.thread import ThreadPoolExecutor
from platform import architecture
from threading import Thread
from uuid import uuid4
Expand All @@ -24,6 +25,8 @@

logger = logging.getLogger('pywebview')

executor = ThreadPoolExecutor()


class WebViewException(Exception):
pass
Expand Down Expand Up @@ -84,23 +87,20 @@ def generate_func():


def js_bridge_call(window, func_name, param):
def _call():
result = json.dumps(func(func_params)).replace('\\', '\\\\').replace('\'', '\\\'')
code = 'window.pywebview._returnValues["{0}"] = {{ isSet: true, value: \'{1}\'}}'.format(func_name, result)
window.evaluate_js(code)

func = getattr(window.js_api, func_name, None)
if func is None:
logger.error('Function {}() does not exist'.format(func_name))
return

if func is not None:
def done_callback(future):
try:
func_params = param if not param else json.loads(param)
t = Thread(target=_call)
t.start()
result = json.dumps(future.result()).replace('\\', '\\\\').replace('\'', '\\\'')
code = 'window.pywebview._returnValues["{0}"] = {{ isSet: true, value: \'{1}\'}}'.format(func_name, result)
window.evaluate_js(code)
except Exception:
logger.exception('Error occurred while evaluating function {0}'.format(func_name))
else:
logger.error('Function {}() does not exist'.format(func_name))

executor.submit(func, param and json.loads(param)).add_done_callback(done_callback)

def escape_string(string):
return string\
Expand Down