Skip to content
Back to skills

Python Asyncio Gui Threading

ASecurity

Run async code alongside CustomTkinter GUI without crashes

  • 2 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 10, 2026
ai-agentspython

Security analysis

A100/100

Scanned September 10, 2026

npx -y skills add LoopyLuci/Skills --skill python-asyncio-gui-threading --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Python Asyncio Gui Threading?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Python Asyncio Gui Threading
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/loopyluci-python-asyncio-gui-threading/badge)](https://www.skillsdirectory.com/skills/loopyluci-python-asyncio-gui-threading)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: python-asyncio-gui-threading
description: "Run async code alongside CustomTkinter GUI without crashes"
---

# Python Asyncio + GUI Threading

Running async network code alongside a synchronous GUI requires careful threading.

## Architecture

Main thread (GUI) + background thread (asyncio) + queue to bridge them.

## Pattern

```python
import asyncio, threading, queue
import customtkinter as ctk

class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        self._loop = None
        self._async_queue = queue.Queue()
        self._start_async()
        self.after(100, self._poll)

    def _start_async(self):
        def run():
            self._loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self._loop)
            self._loop.run_until_complete(self._async_main())
        threading.Thread(target=run, daemon=True).start()

    async def _async_main(self):
        await asyncio.sleep(1)

    def _schedule(self, fn):
        self._async_queue.put(fn)

    def _poll(self):
        try:
            while True:
                self._async_queue.get_nowait()()
        except queue.Empty:
            pass
        self.after(50, self._poll)

    def _run_async(self, coro):
        asyncio.run_coroutine_threadsafe(coro, self._loop)
```

## Critical Rule

Never call `get_event_loop()` from callbacks. Store the loop:

```python
# WRONG — crashes from mDNS/timer threads
asyncio.run_coroutine_threadsafe(coro(), asyncio.get_event_loop())

# RIGHT — use stored reference
self._loop = loop
asyncio.run_coroutine_threadsafe(coro(), self._loop)
```

## Shutdown

```python
def destroy(self):
    self._cleanup()
    try:
        super().destroy()
    except tkinter.TclError:
        pass  # Already destroyed
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…