Skip to content

Instantly share code, notes, and snippets.

@Bomberus
Last active November 8, 2019 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bomberus/0000ef0d56ee0aac285d9e2045452b0b to your computer and use it in GitHub Desktop.
Save Bomberus/0000ef0d56ee0aac285d9e2045452b0b to your computer and use it in GitHub Desktop.
Asyncqt, concurrent executor example
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())
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