Skip to content

Commit 29a7cff

Browse files
[Edit] Python - .Thread() (#6565)
* Update thread.md * Update thread.md * minor fixes, fixed lint, added backlinks, changed some content ---------
1 parent c727758 commit 29a7cff

File tree

1 file changed

+33
-18
lines changed
  • content/python/concepts/threading/terms/thread

1 file changed

+33
-18
lines changed

content/python/concepts/threading/terms/thread/thread.md

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
---
22
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.'
44
Subjects:
55
- 'Computer Science'
66
- 'Data Science'
77
Tags:
88
- 'Async Await'
99
- 'Functions'
10+
- 'Thread'
1011
CatalogContent:
1112
- 'learn-python-3'
1213
- 'paths/computer-science'
1314
---
1415

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.
1617

1718
## Syntax
1819

@@ -22,7 +23,7 @@ threading.Thread(target=callable, args=())
2223

2324
[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`.
2425

25-
## Example
26+
## Example: Basic Thread Creation
2627

2728
The object that returns from the `.Thread()` constructor can be assigned to its own variable, as shown in the example below:
2829

@@ -43,9 +44,9 @@ Every thread object has a `name` attribute that, unless otherwise specified, def
4344
<Thread(Thread-2, initial)>
4445
```
4546

46-
## Codebyte Example 1
47+
## Codebyte Example 1: Simple Greeting Thread
4748

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:
4950

5051
```codebyte/python
5152
import threading
@@ -59,18 +60,18 @@ hello_thread = threading.Thread(target=say_hello, args=("World",))
5960
hello_thread.start()
6061
```
6162

62-
## Codebyte Example 2
63+
## Codebyte Example 2: Concurrent File Downloads
6364

64-
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:
6566

6667
```codebyte/python
6768
import threading
6869
import time
6970
7071
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}")
7475
7576
# Creating two threads to simulate downloading two files simultaneously
7677
thread_1 = threading.Thread(target=download_file, args=("file1.txt", 2))
@@ -87,23 +88,23 @@ thread_2.join()
8788
print("Both downloads completed!")
8889
```
8990

90-
## Codebyte Example 3
91+
## Codebyte Example 3: Parallel Task Execution
9192

92-
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:
9394

9495
```codebyte/python
9596
import threading
9697
import time
9798
9899
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!")
102103
103104
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!")
107108
108109
# Creating threads for making coffee and toasting bread
109110
coffee_thread = threading.Thread(target=make_coffee)
@@ -119,3 +120,17 @@ toast_thread.join()
119120
120121
print("Breakfast is ready!")
121122
```
123+
124+
## Frequently Asked Questions
125+
126+
### 1. How to sleep a thread in Python?
127+
128+
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

Comments
 (0)