Run on main thread python

What is the Main Thread in Python

The main thread is the default thread within a Python process.

In this tutorial you will discover the main thread and how to access it.

What is the Main Thread?

Each Python process is created with one default thread referred to as the “main thread“.

When you execute a Python program, it is executing in the main thread.

The main thread can be thought of as the default thread within your Python process.

In normal conditions, the main thread is the thread from which the Python interpreter was started.

— threading — Thread-based parallelism

The main thread in each Python process always has the name “MainThread” and is not a daemon (background) thread.

This means that once the main thread exits, the Python process will exit, assuming there are no other non-daemon threads running.

There is a “main thread” object; this corresponds to the initial thread of control in the Python program. It is not a daemon thread.

— threading — Thread-based parallelism

Now that we know what the main thread is, let’s look at how we might access it.

Run your loops using all CPUs, download my FREE book to learn how.

How to Access the Main Thread

Each process in Python has a threading.Thread instance associated with it.

Читайте также:  Program menu in python

It can be helpful to retrieve the threading.Thread instance for the main thread so that it can be queried, such as for debugging or logging purposes. Attributes such as the name and the identifier and the native thread identifier can be retrieved.

There are two main ways to access the main thread.

  • threading.current_thread(): Get a threading.Thread instance for the current thread.
  • threading.main_thread(): Get the threading.Thread for the main thread.

We can acquire a threading.Thread instance that represents the main thread by calling the threading.current_thread() function from within the main thread.

Recall that the main thread is the default thread you code is running within in any Python process.

Источник

How to Run a Function in a New Thread in Python

You can run a function in a new thread via the “target” argument on the threading.Thread class.

In this tutorial you will discover how to run a function in a new thread.

Need to Run A Function in a New Thread

A thread is a thread of execution in a computer program.

Every Python program has at least one thread of execution referred to as the main thread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create additional threads in our program in order to execute code concurrently.

Python provides the ability to create and manage new threads via the threading.Thread class.

One way of running a function in a new thread is via an argument on the threading.Thread class.

How can we run a function in a new thread using the threading.Thread class?

Run your loops using all CPUs, download my FREE book to learn how.

Читайте также:  Run python scripts with arguments

How to Run a Function In a Thread

To run a function in another thread:

  1. Create an instance of the threading.Thread class.
  2. Specify the name of the function via the “target” argument.
  3. Call the start() function.

First, we must create a new instance of the threading.Thread class and specify the function we wish to execute in a new thread via the “target” argument.

Источник

Оцените статью