You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/python/concepts/threading/terms/thread/thread.md
+33-18
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,19 @@
1
1
---
2
2
Title: '.Thread()'
3
-
Description: 'Returns a thread object that can run a function with zero or more arguments.'
3
+
Description: 'Creates a new thread that runs a target function with optional arguments.'
4
4
Subjects:
5
5
- 'Computer Science'
6
6
- 'Data Science'
7
7
Tags:
8
8
- 'Async Await'
9
9
- 'Functions'
10
+
- 'Thread'
10
11
CatalogContent:
11
12
- 'learn-python-3'
12
13
- 'paths/computer-science'
13
14
---
14
15
15
-
The **`.Thread()`**method is a class constructor that returns a thread object that can run a function with zero or more arguments.
16
+
The **`.Thread()`**constructor from Python's [**threading**](https://www.codecademy.com/resources/docs/python/threading) module creates a thread object that can run a specified function with optional arguments.
[Functions](https://www.codecademy.com/resources/docs/python/functions) are commonly passed as the `target` argument, but without parentheses. If any items are listed in the `args` tuple, they are passed as positional arguments to the `target`.
24
25
25
-
## Example
26
+
## Example: Basic Thread Creation
26
27
27
28
The object that returns from the `.Thread()` constructor can be assigned to its own variable, as shown in the example below:
28
29
@@ -43,9 +44,9 @@ Every thread object has a `name` attribute that, unless otherwise specified, def
43
44
<Thread(Thread-2, initial)>
44
45
```
45
46
46
-
## Codebyte Example 1
47
+
## Codebyte Example 1: Simple Greeting Thread
47
48
48
-
In the example below, a thread, `hello_thread`, targets the `say_hello()` function with supplied arguments. After the thread is created, the targeted `say_hello()` function is executed when the [`.start()`](https://www.codecademy.com/resources/docs/python/threading/start) method is run.
49
+
In the example below, a thread, `hello_thread`, targets the `say_hello()` function with supplied arguments. After the thread is created, the targeted `say_hello()` function is executed when the [`.start()`](https://www.codecademy.com/resources/docs/python/threading/start) method is run:
In the example below, two threads, `thread_1` and `thread_2`, target the `download_file()` function with supplied arguments. Each thread simulates downloading a file concurrently by running the `download_file()` function in the background. After the threads are created, the targeted `download_file()` functions are executed when the `.start()` method is run.
65
+
In the example below, two threads, `thread_1` and `thread_2`, target the `download_file()` function with supplied arguments. Each thread simulates downloading a file concurrently by running the `download_file()` function in the background. After the threads are created, the targeted `download_file()` functions are executed when the `.start()` method is run:
65
66
66
67
```codebyte/python
67
68
import threading
68
69
import time
69
70
70
71
def download_file(filename, duration):
71
-
print(f"Starting download: {filename}")
72
-
time.sleep(duration)
73
-
print(f"Finished downloading: {filename}")
72
+
print(f"Starting download: {filename}")
73
+
time.sleep(duration)
74
+
print(f"Finished downloading: {filename}")
74
75
75
76
# Creating two threads to simulate downloading two files simultaneously
In the example below, two threads, `coffee_thread` and `toast_thread`, target the `make_coffee()` and `toast_bread()` functions, respectively. Each thread simulates the preparation of coffee and toast concurrently. After the threads are created, the targeted functions are executed when the `.start()` method is run.
93
+
In the example below, two threads, `coffee_thread` and `toast_thread`, target the `make_coffee()` and `toast_bread()` functions, respectively. Each thread simulates the preparation of coffee and toast concurrently. After the threads are created, the targeted functions are executed when the [`.start()`](https://www.codecademy.com/resources/docs/python/threading/start) method is run:
93
94
94
95
```codebyte/python
95
96
import threading
96
97
import time
97
98
98
99
def make_coffee():
99
-
print("Making coffee...")
100
-
time.sleep(3) # Simulating the time taken to make coffee
101
-
print("Coffee is ready!")
100
+
print("Making coffee...")
101
+
time.sleep(3) # Simulating the time taken to make coffee
102
+
print("Coffee is ready!")
102
103
103
104
def toast_bread():
104
-
print("Toasting bread...")
105
-
time.sleep(2) # Simulating the time taken to toast bread
106
-
print("Bread is toasted!")
105
+
print("Toasting bread...")
106
+
time.sleep(2) # Simulating the time taken to toast bread
107
+
print("Bread is toasted!")
107
108
108
109
# Creating threads for making coffee and toasting bread
To sleep a thread in Python, use the [`time.sleep()`](https://www.codecademy.com/resources/docs/python/time-module/sleep) function. Import the [`time`](https://www.codecademy.com/resources/docs/python/time-module) module and call `time.sleep(seconds)` where `seconds` is the number of seconds to pause the thread. This is useful for adding delays, simulating wait times, or creating periodic tasks in Python's threading.
129
+
130
+
### 2. Is Pandas single-threaded?
131
+
132
+
[Pandas](https://www.codecademy.com/resources/docs/pandas) is primarily single-threaded by default. However, some functions (like [`read_csv()`](https://www.codecademy.com/resources/docs/pandas/built-in-functions/read-csv) with `engine='pyarrow'`) can use multi-threading or multi-processing for performance.
133
+
134
+
### 3. Is Node.js single-threaded?
135
+
136
+
[Node.js](https://www.codecademy.com/resources/blog/what-is-nodejs/) operates on a single-threaded event loop model but handles concurrent operations through asynchronous callbacks. For true parallelism in Node.js, you can use the Worker Threads API or the cluster module.
0 commit comments