-
-
Save VolkerH/128ae5f9e8031a49b0600bec4ee31e52 to your computer and use it in GitHub Desktop.
Asyncqt, concurrent executor example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, asyncio, random, threading, datetime | |
from qtpy.QtWidgets import QApplication, QProgressBar | |
from asyncqt import QEventLoop, QThreadExecutor | |
app = QApplication(sys.argv) | |
loop = QEventLoop(app) | |
asyncio.set_event_loop(loop) | |
progress = QProgressBar() | |
progress.show() | |
async def do_request(value): | |
print(f"Running in Thread: {threading.get_ident()}") | |
#print("do request") | |
await asyncio.sleep(value) | |
#print("request finished") | |
return value | |
def working(i): | |
#print("working") | |
#Create a new working task loop | |
work_loop = asyncio.new_event_loop() | |
result = work_loop.run_until_complete(do_request(i)) | |
#Update progressbar in main loop | |
loop.call_soon_threadsafe(progress.setValue, progress.value() + 20) | |
#print("finished working") | |
return result | |
async def master(): | |
print("exec") | |
#Init run 1 Thread | |
start = datetime.datetime.now() | |
result_list = [] | |
loop.call_soon_threadsafe(progress.setValue, 0) | |
with QThreadExecutor(1) as executor: | |
result_list = await asyncio.gather(*[loop.run_in_executor(executor, working, i) for i in range(0, 5)]) | |
end = datetime.datetime.now() | |
print(result_list) | |
print(f"1 Thread took: {(end - start).total_seconds()}") | |
#Init run 2 Thread | |
start = datetime.datetime.now() | |
result_list = [] | |
loop.call_soon_threadsafe(progress.setValue, 0) | |
with QThreadExecutor(2) as executor: | |
result_list = await asyncio.gather(*[loop.run_in_executor(executor, working, i) for i in range(0, 5)]) | |
end = datetime.datetime.now() | |
print(result_list) | |
print(f"2 Thread took: {(end - start).total_seconds()}") | |
#Init run 4 Thread | |
start = datetime.datetime.now() | |
result_list = [] | |
loop.call_soon_threadsafe(progress.setValue, 0) | |
with QThreadExecutor(4) as executor: | |
result_list = await asyncio.gather(*[loop.run_in_executor(executor, working, i) for i in range(0, 5)]) | |
end = datetime.datetime.now() | |
print(result_list) | |
print(f"8 Thread took: {(end - start).total_seconds()}") | |
print("master finished") | |
with loop: | |
loop.run_until_complete(master()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running in Thread: 139783300953856 | |
Running in Thread: 139783300953856 | |
Running in Thread: 139783300953856 | |
Running in Thread: 139783300953856 | |
Running in Thread: 139783300953856 | |
[0, 1, 2, 3, 4] | |
1 Thread took: 10.037408 | |
Running in Thread: 139783292561152 | |
Running in Thread: 139783300953856 | |
Running in Thread: 139783292561152 | |
Running in Thread: 139783300953856 | |
Running in Thread: 139783292561152 | |
[0, 1, 2, 3, 4] | |
2 Thread took: 6.020507 | |
Running in Thread: 139783292561152 | |
Running in Thread: 139782663436032 | |
Running in Thread: 139783284168448Running in Thread: 139782655043328 | |
Running in Thread: 139783284168448 | |
[0, 1, 2, 3, 4] | |
8 Thread took: 4.015765 | |
master finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment