threading.current_thread().name
时间: 2024-08-15 19:03:57 浏览: 47
`threading.current_thread().name` 是 Python 中 `threading` 模块中的一个属性,用于获取当前线程的名字。当你在一个多线程环境中,通过 `current_thread()` 函数可以获取到运行当前代码的线程对象,然后 `.name` 属性会返回该线程所设置的名字,如果没有显式设置名字,默认是一个默认的名称。
例如,如果你创建了一个线程并设置了名字:
```python
import threading
def worker():
print(threading.current_thread().name)
my_thread = threading.Thread(target=worker, name="MyThread")
my_thread.start()
```
这里 `my_thread.name` 将会被设置为 "MyThread",而当它开始运行并在主线程中打印时,将显示 "MyThread"。
相关问题
import sys import threading import time from PyQt5.QtWidgets import * from PyQt5 import uic import pandas as pd import random # import pyqtgraph as pg import matplotlib.pyplot as plt from PyQt5.QtWidgets import QGroupBox from PyQt5 import QtWidgets from login_4 import Ui_CK from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas # df = pd.read_excel('shu.xlsx') class MyWindow(QWidget and QMainWindow,Ui_CK): def __init__(self): super().__init__() self.init_ui() groupbox = QGroupBox('Title',self) # self.plot = pg.PlotWidget(enableAutoRange=True) # self.ui.verticalLayout.addWidget(self.plot) # self.curve = self.plot.plot() #self.ui = uic.loadUi("./login_4.ui") def init_ui(self): print('1.1') try: self.ui = uic.loadUi("./login_4.ui") #print(threading.current_thread()) #print(self.ui.__dict__) # print(self.ui.label) # print(self.ui.label.text()) # 查看ui文件中有哪些控件 # 提取要操作的控件 self.user_name_qwidget = self.ui.lineEdit # 单位输入框 self.password_qwidget = self.ui.lineEdit_2 # 二级单位输入框 self.zhicheng_qwidget = self.ui.lineEdit_3 # 职称输入框 self.jiaoyuan_qwidget = self.ui.lineEdit_4 # 教员输入框 self.login_btn = self.ui.pushButton # 登录抽课按钮 self.textBrowser = self.ui.textBrowser # 授课对象显示区域 # 绑定信号与槽函数 self.textBrowser_2 = self.ui.textBrowser_2 # 文本显示区域课程名称 self.textBrowser_3 = self.ui.textBrowser_3 # 文本显示区域课次 self.textBrowser_4 = self.ui.textBrowser_4 # 文本显示区域教研室 self.login_btn.clicked.connect(self.login) self.login_btna = self.ui.pushButton_2 self.login_btna.clicked.connect(lambda: self.plot_q()) except Exception as e: print(e.__class__.__name__, e) def login(self): print('1.2') """登录按钮的槽函数""" #print(self.user_name_qwidget.text()) a = self.user_name_qwidget.text() e = sel 为什么会报错
这是一段Python代码,通过使用import关键字导入了sys、threading、time等模块。同时,也导入了PyQt5.QtWidgets的部分类和PyQt5.uic模块。此外,还导入了pandas和random模块,用于处理数据和生成随机数等操作。
import requests from bs4 import BeautifulSoup import threading import time headers = { "User-Agent": 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/90.0.4430.212 Safari/537.36' } def download(url): start_time = time.time() # 记录开始时间 response = requests.get(url, headers=headers).text soup = BeautifulSoup(response, features='lxml') src = soup.find_all('img') imagesrc = soup.find_all('img', width="100") for s in imagesrc: with open("{}.jpg".format(s.get('alt')), 'wb') as file: image = requests.get(s.get('src')).content file.write(image) print("正在下载" + s.get('alt') + '.jpg') end_time = time.time() # 记录结束时间 print("线程 {} 运行时间为:{} 秒".format(threading.current_thread().name, end_time - start_time)) threads = [] for x in range(10): url = "https://movie.douban.com/top250?start={}&filter=".format(x * 25) thread = threading.Thread(target=download, args=(url,), name="Thread-{}".format(x+1)) threads.append(thread) thread.start() for thread in threads: thread.join()改为单线程
import requests from bs4 import BeautifulSoup import time headers = { "User-Agent": 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/90.0.4430.212 Safari/537.36' } def download(url): start_time = time.time() # 记录开始时间 response = requests.get(url, headers=headers).text soup = BeautifulSoup(response, features='lxml') src = soup.find_all('img') imagesrc = soup.find_all('img', width="100") for s in imagesrc: with open("{}.jpg".format(s.get('alt')), 'wb') as file: image = requests.get(s.get('src')).content file.write(image) print("正在下载" + s.get('alt') + '.jpg') end_time = time.time() # 记录结束时间 print("运行时间为:{} 秒".format(end_time - start_time)) url_list = [] for x in range(10): url = "https://movie.douban.com/top250?start={}&filter=".format(x * 25) url_list.append(url) for url in url_list: download(url)
阅读全文