From f09fdf3ee5eaec3f7638cde03063ca1252302def Mon Sep 17 00:00:00 2001 From: Stavros Korokithakis Date: Wed, 19 Jul 2023 00:41:53 +0300 Subject: [PATCH] Updates --- content/SUMMARY.md | 2 + content/programming/index.md | 5 +++ ...orator-that-runs-a-function-in-a-thread.md | 38 +++++++++++++++++++ content/python/index.md | 5 +++ 4 files changed, 50 insertions(+) create mode 100644 content/programming/index.md create mode 100644 content/python/decorator-that-runs-a-function-in-a-thread.md create mode 100644 content/python/index.md diff --git a/content/SUMMARY.md b/content/SUMMARY.md index d704759..e1c840a 100644 --- a/content/SUMMARY.md +++ b/content/SUMMARY.md @@ -41,6 +41,8 @@ - [GRBL_ESP32 tips](maker-things/grbl-esp32-tips.md) - [How to properly level your 3D printer](maker-things/how-to-properly-level-your-3d-printer.md) - [Installing BLTouch-compatible firmware onto the TwoTrees Sapphire Pro](maker-things/installing-bltouch-compatible-firmware-onto-the-twotrees-sapphire-pro.md) + - [Python](python/index.md) + - [Decorator that runs a function in a thread](python/decorator-that-runs-a-function-in-a-thread.md) - [Software](software/index.md) - [Black pills](software/black-pills.md) - [Getting VoWiFi working on Xiaomi.eu](software/getting-vowifi-working-on-xiaomi-eu.md) diff --git a/content/programming/index.md b/content/programming/index.md new file mode 100644 index 0000000..19b5810 --- /dev/null +++ b/content/programming/index.md @@ -0,0 +1,5 @@ +# Contents + +Click on a link in the list below to go to that page: + +1. diff --git a/content/python/decorator-that-runs-a-function-in-a-thread.md b/content/python/decorator-that-runs-a-function-in-a-thread.md new file mode 100644 index 0000000..b0233f8 --- /dev/null +++ b/content/python/decorator-that-runs-a-function-in-a-thread.md @@ -0,0 +1,38 @@ +# Decorator that runs a function in a thread + +I wrote a small decorator that will convert any function to run in a thread: + + +``` +import threading + +def run_threaded(fn): + """A decorator that makes a function run in a thread.""" + + def run(*k, **kw): + t = threading.Thread(target=fn, args=k, kwargs=kw) + t.start() + return t + + return run +``` + +Example: + +``` +@run_threaded +def add(x, y): + # This runs in a separate thread. + print(x+y) + +add(1+2) +``` + +That's it! + +* * * + +

+Last updated on July 19, 2023. For any questions/feedback, +email me at hi@stavros.io. +

diff --git a/content/python/index.md b/content/python/index.md new file mode 100644 index 0000000..101dd60 --- /dev/null +++ b/content/python/index.md @@ -0,0 +1,5 @@ +# Contents + +Click on a link in the list below to go to that page: + +1. [Decorator that runs a function in a thread](../../python/decorator-that-runs-a-function-in-a-thread.html)