首页 > 编程语言 >python 异步回调传递参数 warning cell-var-from-loop

python 异步回调传递参数 warning cell-var-from-loop

时间:2024-01-21 12:44:41浏览次数:17  
标签:img python li cell url warning path loop lambda

warning : cell-var-from-loop

bing解释

for i in range(10):
    f = lambda i:i 
    print(f())

The warning message cell-var-from-loop is emitted by Pylint, a Python code analysis tool. This warning is raised when a variable is defined inside a loop and used in a closure.

The warning message indicates that this will result in all closures using the same value for the closed-over variable ².

In your code, the warning message is raised because the variable i is defined inside a loop and used in a closure.

To fix this warning, you can use a lambda function as a closure and pass the value of i as a parameter to the lambda function. Here is an example of how you can modify your code:

for i in range(10):
    f = lambda x=i: x
    print(f())

In this example, the lambda function takes i as a parameter and returns its value.

By doing so, the value of i is passed to the lambda function as a parameter and each closure uses a different value for the closed-over variable.

example

# task.add_done_callback(lambda futrue: self.get_image_url(futrue, i, li))
task.add_done_callback(lambda futrue, idx=i, img_path=li: self.get_image_url(futrue, idx, img_path))

tasks = []
for i, li in enumerate(self.images):
    if li.startswith("/"):  # 本地图片
        li = "." + li
    image_full_path = os.path.join(self.base_dir, li)
    if not os.path.exists(image_full_path):
        print(f"图片{image_full_path}不存在")
        continue
    task = asyncio.ensure_future(self.__upload_md_img(image_full_path))
    # task.add_done_callback(lambda futrue: self.get_image_url(futrue, i, li))
    task.add_done_callback(lambda futrue, idx=i, img_path=li: self.get_image_url(futrue, idx, img_path))
    tasks.append(task)
if len(tasks) == 0:
    return
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()


def get_image_url(self, t: asyncio.Task, idx, img_path):
    """回调,获取url"""
    img_url = t.result()
    print(f"{idx} {img_path}上传成功,url:{img_url}")
    self.net_images.append(img_url)

标签:img,python,li,cell,url,warning,path,loop,lambda
From: https://www.cnblogs.com/zhuoss/p/17977722

相关文章

  • 软件测试|探索Flask接口路由技术:构建灵活可拓展的Python应用
    测试管理班是专门面向测试与质量管理人员的一门课程,通过提升从业人员的团队管理、项目管理、绩效管理、沟通管理等方面的能力,使测试管理人员可以更好的带领团队、项目以及公司获得更快的成长。提供1v1私教指导,BAT级别的测试管理大咖量身打造职业规划。什么是路由路由是将URL地......
  • Python Matplotlib 多个坐标系下绘制多个图像
    ​ 1、绘制图像使用 plt.subplots()可以创建一个图形对象以及一个或多个子图(axes)对象。使得在同一个窗口中绘制多个图像变得非常简单和直观。使用 plt.subplots(),可以轻松地管理多个子图的布局,并且可以对每个子图进行独立的绘图和自定义设置。常用参数如下,参数说明......
  • 【办公自动化】Python执行Windows命令
    ##第一部分:引言在Python中,我们可以使用`os`模块来执行Windows命令。`os`模块提供了许多与操作系统交互的函数,包括执行系统命令。以下是一个简单的示例,展示了如何使用Python执行Windows命令。##第二部分:导入os模块首先,我们需要导入`os`模块。在Python脚本的开头添加以下代码:`......
  • 【办公自动化】Python执行Windows命令
    第一部分:引言在Python中,我们可以使用os模块来执行Windows命令。os模块提供了许多与操作系统交互的函数,包括执行系统命令。以下是一个简单的示例,展示了如何使用Python执行Windows命令。第二部分:导入os模块首先,我们需要导入os模块。在Python脚本的开头添加以下代码:importos第......
  • Python实战:selenium模拟浏览器运行,获取软科网站2023中国大学排名
    Python实战:selenium模拟浏览器运行,获取软科网站2023中国大学排名在爬取一些加密的网页时,可以使用selenium模拟浏览器运行,再从网页中提取想要的数据。使用的库本文使用到的Python库有:selenium、bs4、pandas使用selenium解决网页的反爬使用bs4对html网页进行解析和提取数据......
  • python+flask_restful使用jwt
    正在做毕设,由于毕设需求使用python+flask,使用jwt目的是对请求进行保护,我的项目采用vue3+flask前后端分离实现,进入正题:使用jwt,首先下载扩展pipinstallFlask-JWT-Extended然后在项目中注册使用,我的项目结构是这样的 在exts.py中引入jwt扩展,fromflask_jwt_extendedimpo......
  • Python实现线性判别分析鸢尾花数据集或随机生成两个线性可分的数据集
    线性判别分析是一种经典的线性学习方法,在二分类问题上最早由Fisher在1936年提出,亦称Fisher线性判别。线性判别的思想非常朴素:给定训练样例集,设法将样例投影到一条直线上,使得同类样例的投影点尽可能接近,异类样例的投影点尽可能远离;在对新样本进行分类时,将其投影到同样的直线上,再根据......
  • Python中的回调函数
    先来看一个程序:deff1():print(2)return1deff2(a):print(3)returnaprint(f2(f1()))这个程序,在调用时,f2会先等待f1调用完毕,返回1之后,再进行调用,所以会输出2、3、1,但是若这样改写程序deff1():print(2)return1deff2(f):prin......
  • 【Python】查找和删除表格中的重复行
    查找:importpandasaspddf=pd.read_excel("秘籍.xlsx")#打开list1=[]foriinrange(df.shape[0]):#总行数list1.append(list(df.values[i]))#将每行以列表的形式装入列表forxinlist1:iflist1.count(x)>1:#如果列表中元素出现次数>1prin......
  • Python手相识别教程10命运线
    10命运线土星线是手相中信息量最大的线条之一。它记录了工作和生活方式的重大变化,描述了我们在人生不同阶段的安全感。这条线有很多名字:命运线、命运线,以及最贴切的安全线。命运线反映了货币安全,但这并不是土星线上显示的唯一一种安全。这条线的标记和特征可能是客观的,也可能......