question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method to run code in a thread

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ksamuelcommented, Sep 4, 2021

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:

  • get_event_loop automatically
  • use the default executor instead of requesting None as a first parameter
  • wrap the callable and any excess param into functiools.partial prior to passing it to the thread
  • if the callable returns a coroutine or if passed an awaitable, we call create_task from a different event loop within the wrapper and pass it to run_until_complete

What’s message pump ?

Edit : got it, message pump seems to be a queue based pub/sub infra to propagate event in the app.

1reaction
tusharsadhwanicommented, Sep 30, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found