Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Videos & KonferenzenTwo Minute Papers: Claude Opus 5.5 AI: A Massive Leap Forward(24.09.2026 um 10:40 Uhr)
Sicherheitslücken (CVE)USN-8805-1: Moodle vulnerability(23.09.2026 um 16:43 Uhr)
Sichere ProgrammierungI thought clipboard sync would be simple. Android had other plans.(24.09.2026 um 11:01 Uhr)
Sichere ProgrammierungAI-assisted genealogy, a follow-up(24.09.2026 um 11:02 Uhr)
Sicherheitslücken (CVE)Smart Contract Vulnerability Surface Analysis: HashKey Exchange(24.09.2026 um 11:02 Uhr)
Sichere ProgrammierungAI Agents Calling Your Existing Backend Without MCP Development(24.09.2026 um 11:06 Uhr)
Videos & KonferenzenTwo Minute Papers: Claude Opus 5.5 AI: A Massive Leap Forward(24.09.2026 um 10:40 Uhr)
Sicherheitslücken (CVE)USN-8805-1: Moodle vulnerability(23.09.2026 um 16:43 Uhr)
Sichere ProgrammierungI thought clipboard sync would be simple. Android had other plans.(24.09.2026 um 11:01 Uhr)
Sichere ProgrammierungAI-assisted genealogy, a follow-up(24.09.2026 um 11:02 Uhr)
Sicherheitslücken (CVE)Smart Contract Vulnerability Surface Analysis: HashKey Exchange(24.09.2026 um 11:02 Uhr)
Sichere ProgrammierungAI Agents Calling Your Existing Backend Without MCP Development(24.09.2026 um 11:06 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Python Concurrency: A Guide to Threads, Processes, and Asyncio

Your Python script needs to do multiple things at once to be faster. But how? Python offers a rich but sometimes confusing landscape of concurrency and parallelism tools. Should you use threads, processes, or asyncio? Choosing the right…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Your Python script needs to do multiple things at once to be faster. But how? Python offers a rich but sometimes confusing landscape of concurrency and parallelism tools. Should you use threads, processes, or asyncio?



Choosing the right tool for the job is the key to writing efficient, scalable code. This guide will walk you through the three main concurrency models in Python, explaining what they are, how to use them, and when to choose each one.






The Foundation: concurrent.futures



For traditional, blocking code, Python's concurrent.futures module provides a beautiful, high-level API for managing pools of threads and processes. It introduces the "Executor" pattern, where you submit jobs to a pool and retrieve the results.






1. ThreadPoolExecutor: For I/O-Bound Work



When to use it: When your task spends most of its time waiting for external resources. This is called I/O-bound work. Examples include:




  • Making network requests (e.g., calling APIs, scraping websites).

  • Reading or writing from a slow disk.

  • Querying a database.



Due to Python's Global Interpreter Lock (GIL), threads are not suitable for speeding up CPU-heavy tasks, but they are perfect for I/O-bound tasks because they can "wait" concurrently.



Syntax Example: Let's download the content of several web pages.




import requests
from concurrent.futures import ThreadPoolExecutor

URLS = [
"https://www.python.org/",
"https://www.djangoproject.com/",
"https://flask.palletsprojects.com/",
]

def fetch_url(url: str):
print(f"Fetching {url}...")
response = requests.get(url)
print(f"Fetched {url} with status {response.status_code}")
return len(response.content)

with ThreadPoolExecutor(max_workers=5) as executor:
# The map function runs `fetch_url` for each item in URLS
results = executor.map(fetch_url, URLS)

for url, length in zip(URLS, results):
print(f"URL: {url}, Length: {length}")






The thread pool allows all three requests.get() calls to happen concurrently, dramatically speeding up the total execution time.






2. ProcessPoolExecutor: For CPU-Bound Work



When to use it: When your task is doing heavy computation and maxing out a CPU core. This is called CPU-bound work. Examples include:




  • Complex mathematical calculations.

  • Image or video processing.

  • Data analysis on large datasets.



Processes run in separate memory spaces and have their own Python interpreter, which allows them to bypass the GIL and run on different CPU cores in true parallel.



Syntax Example: Let's perform a heavy calculation on a list of numbers.




from concurrent.futures import ProcessPoolExecutor

def heavy_calculation(num: int):
print(f"Calculating for {num}...")
# A silly, CPU-intensive task
return sum(i * i for i in range(num))

numbers_to_process = [100_000, 150_000, 200_000]

with ProcessPoolExecutor() as executor:
results = executor.map(heavy_calculation, numbers_to_process)

for num, result in zip(numbers_to_process, results):
print(f"Calculation for {num} returned {result}")






Notice the syntax is nearly identical to the thread pool example! concurrent.futures makes it easy to switch between them.






3. The Third Model: asyncio



What if your code is already async and uses libraries like httpx or asyncpg? In this case, you don't need threads or processes. asyncio has its own, more efficient tools for managing I/O-bound concurrency.






Managing Async Tasks: gather and TaskGroup



Let's say we have a simple async function:




async def say_after(delay: int, what: str):
await asyncio.sleep(delay)
print(what)









asyncio.gather



gather is a high-level utility to run multiple awaitable objects concurrently.



Gathering Coroutines (most common):

You can pass coroutine objects directly to gather. It will automatically schedule them as tasks.




import asyncio

# This runs both `say_after` calls concurrently
await asyncio.gather(
say_after(1, "hello"),
say_after(2, "world")
)






Gathering Tasks:

You can also create tasks explicitly with asyncio.create_task and then gather them. This gives you more control if you need to interact with the Task objects before they are complete.




task1 = asyncio.create_task(say_after(1, "hello"))
task2 = asyncio.create_task(say_after(2, "world"))
await asyncio.gather(task1, task2)









asyncio.TaskGroup (The Modern, Safe Way)



Introduced in Python 3.11, TaskGroup is a modern and safer way to manage concurrent tasks. It uses a context manager (async with) to create a scope, guaranteeing that all tasks created within it are awaited before the block is exited.



Benefits:





  • Structured Concurrency: No more "forgotten" tasks that run in the background.


  • Superior Exception Handling: If any task in the group fails, all other tasks are automatically cancelled.



Syntax Example:




async with asyncio.TaskGroup() as tg:
tg.create_task(say_after(1, "hello"))
tg.create_task(say_after(2, "world"))

print("Both tasks have now completed.")






For new asyncio code, TaskGroup is generally preferred over gather.






Summary: When to Use Which?




























Is your task... And are you using... Your best tool is...
CPU-Bound Any Python code ProcessPoolExecutor
I/O-Bound Blocking libraries (e.g., requests, psycopg2) ThreadPoolExecutor
I/O-Bound
async libraries (e.g., httpx, asyncpg)

asyncio (TaskGroup or gather)





Conclusion



Python provides powerful tools for every concurrency and parallelism need. The key is to correctly identify the nature of your task. By choosing the right model—Processes for CPU-bound work, Threads for blocking I/O, and Asyncio for non-blocking I/O—you can write efficient, scalable, and high-performance applications.

SOC Incident Playbook: Remote Code Execution (RCE) Defense
title: Detect Exploitation - Python Concurrency: A Guide to Threads, Processes, and Asyncio
id: 27c5aeb8-651c-4cfb-a812-efa154c4ffd5
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "Python Concurrency: A Guide to" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Python Concurrency: A Guide to Threads, .... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Python Concurrency: A Guide to Threads, Processes, and Asyncio

Thematisch verwandte Begriffe: Python, Concurrency, Guide, Threads · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-97056 | SigNoz versions from v0.98.0 up to (but not including) v0.143.0, when co…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick