Python3之threading模块

简介: import threading# Tips:一个ThreadLocal变量虽然是全局变量,# 但每个线程都只能读写自己线程的独立副本,互不干扰。# ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题。
import threading

# Tips:一个ThreadLocal变量虽然是全局变量,
# 但每个线程都只能读写自己线程的独立副本,互不干扰。
# ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题。

# 创建全局ThreadLocal对象
local_school = threading.local()

def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello, %s (in %s)' % (std, threading.current_thread().name))

def process_thread(name):
    # 绑定 TheadLocal的Student
    local_school.student = name
    # 调用上面的方法
    process_student()

# 声明一个名字为:Thread-A的线程 执行process_thread方法 传入name为Memor
# Tips: 此处传入的target的方法名称不需要加() 
t1 = threading.Thread(target= process_thread, args=('Memor',),name= 'Thread-A')


# 声明一个名字为:Thread-B的线程 执行process_thread方法 传入name为Godliness
# Tips: 此处传入的target的方法名称不需要加() 
t2 = threading.Thread(target= process_thread, args=('Godliness',),name= 'Thread-B')

# 启动    
t1.start()
t2.start()
t1.join()
t2.join()
目录
相关文章
|
17天前
|
机器学习/深度学习 存储 Python
|
1天前
|
Python
在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存
在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存在,该函数对路径进行检查,存在则返回True,不存在则返回False。示例代码展示了如何检查'example.txt'文件是否存在并相应打印消息。此外,`os.path.isfile()`用于确认路径是否为文件,仅当是文件时返回True,否则返回False,同样配以示例说明其用法。
8 2
|
2天前
|
Python
【Python进阶(五)】——模块搜索及工作目录
【Python进阶(五)】——模块搜索及工作目录
|
4天前
|
Python Windows
python中的异常与模块
python中的异常与模块
9 1
|
13天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{"name": "John", "age": 30, "city": "New York"}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
17 1
|
14天前
|
Python
Python实现压缩解压---tarfile模块详解
Python实现压缩解压---tarfile模块详解
|
14天前
|
Linux Python Windows
Python中time和datetime模块详解
Python中time和datetime模块详解
|
15天前
|
存储 Linux 数据安全/隐私保护
python的压缩模块zipfile详解
python的压缩模块zipfile详解
|
15天前
|
Linux Python Windows
python的os模块详细解读(二)
python的os模块详细解读(二)
|
15天前
|
移动开发 Linux Shell
python的os模块详细解读(一)
python的os模块详细解读(一)
python的os模块详细解读(一)