Created
May 3, 2023 14:05
-
-
Save lllama/39585177cb048135f729134fb0393bb6 to your computer and use it in GitHub Desktop.
Textual multiple workers
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 asyncio | |
from textual import work | |
from textual.app import App | |
from textual.widgets import Button, TextLog | |
class WorkIt(App): | |
def compose(self): | |
yield Button("run_worker", id="run_worker") | |
yield Button("decorators", id="decorators") | |
yield TextLog() | |
def on_button_pressed(self, event): | |
match event.button.id: | |
case "run_worker": | |
self.run_worker(self.task1()) | |
self.run_worker(self.task2()) | |
case "decorators": | |
self.task3() | |
self.task4() | |
async def task1(self): | |
text_log = self.query_one(TextLog) | |
while True: | |
text_log.write("Hello from task 1") | |
await asyncio.sleep(1) | |
async def task2(self): | |
text_log = self.query_one(TextLog) | |
while True: | |
text_log.write("Hello from task 2") | |
await asyncio.sleep(1) | |
@work(exclusive=True) | |
async def task3(self): | |
text_log = self.query_one(TextLog) | |
while True: | |
text_log.write("Hello from task 3") | |
await asyncio.sleep(1) | |
@work(exclusive=True) | |
async def task4(self): | |
text_log = self.query_one(TextLog) | |
while True: | |
text_log.write("Hello from task 4") | |
await asyncio.sleep(1) | |
if __name__ == "__main__": | |
WorkIt().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment