Method to run code in a thread
See original GitHub issueI’d like to add a convenient method to execute in job in a thread.
It would probably be added to message pump (used by app / widgets).
It should allow a method to be called with arbitrary parameters, and should return an awaitable.
await self.call_threaded(my_code, "foo", "bar")
I can see this being used for jobs such as reading from disk (which at the moment blocks the vent loop).
Some considerations: a thread pool may be appropriate. We don’t want this to be unbounded and potentially launch an unlimited number of threads. It should probably work with both async and sync functions, which will require a little inspection. If you want to tackle this, please discuss your ideas here first.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:16 (2 by maintainers)
Top Results From Across the Web
How do I run a simple bit of code in a new thread?
Good place to start reading is Joe Albahari. If you want to create your own thread, this is as simple as it gets:...
Read more >run() Method in Java Thread - GeeksforGeeks
We can use the start() method by using a thread object. Step 1: Create run method. Step 2: Create an object for the...
Read more >How to Run Code in a New Thread in C# - Code Maze
An article presenting a modern way of running run code in a new thread, along with a simple explanation of what happens under...
Read more >How to Run a Function in a New Thread in Python
One way of running a function in a new thread is via an argument on the threading.Thread class. How can we run a...
Read more >Creating and Starting Java Threads - Jenkov.com
There are two ways to specify what code the thread should execute. The first is to create a subclass of Thread and override...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Asyncio already provides facilities for that:
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor
However, we could provide a wrapper that will:
What’s message pump ?
Edit : got it, message pump seems to be a queue based pub/sub infra to propagate event in the app.
I think App.run is important to keep things simple by default, some people don’t know anything about asyncio.
I’d suggest showing both approaches, asyncio one probably in an advanced section somewhere.