1. 使用`threading`模块的`Thread`类:
```python
import threading
def my_function():
线程执行的代码
pass
创建线程对象
thread = threading.Thread(target=my_function)
启动线程
thread.start()
等待线程结束
thread.join()
2. 使用`threading`模块的`start_new_thread`函数:
```python
import threading
def my_function(arg1, arg2):
线程执行的代码
pass
创建线程对象
thread = threading.Thread(target=my_function, args=(arg1, arg2))
启动线程
thread.start()
等待线程结束
thread.join()
3. 继承`threading.Thread`类并重写`run`方法:
```python
import threading
class MyThread(threading.Thread):
def run(self):
线程执行的代码
pass
创建线程对象
thread = MyThread()
启动线程
thread.start()
等待线程结束
thread.join()
以上三种方法都可以用来在Python中创建线程。选择哪种方法取决于你的具体需求和个人偏好