简体   繁体   English

通过控制台启动Python脚本?

[英]Launching Python script through console?

I have written a simple class, that loaded images urls from dictionary IMAGES and downloading and storing these in file storage. 我编写了一个简单的类,该类从字典IMAGES中加载图像URL,然后将其下载并存储在文件存储中。

An code is given below: 代码如下:

class ImageLoader:
    def __init__(self):
        for article, image in IMAGES.items():
            try:
                LOADED_IMAGES[article] = self.loadImage(image, '/home/')
            except BaseException as e:
                ERRORS.append(str(e))
                print("Error load image...." + str(e))

    def nameNameGenerate(self):
        return int(round(time.time() * 1000))

    def extention(self, path):
        ext = path.split(".")[-1]
        return '.' + ext if ext else 'jpg'

    def loadImage(self, path, path_folder):
        filename = str(self.nameNameGenerate()) + str(self.extention(path))
        wget.download(url=path, out=path_folder + filename)
        return '/catalog/s/' + filename

    def save(self):
        for key, value in LOADED_IMAGES.items():
            item = session.query(ProductTable).filter_by(sku=key).one()
            item.image = value
        session.commit()

Using this class: 使用此类:

images = ImageLoader()
images.save()

Problem is I faced is unstable work of script at large data in IMAGES after launching. 问题是启动后我面对的是IMAGES中大数据脚本的不稳定工作。 Let me explain more. 让我解释更多。

When I run it it begins to catch files by URL, iterating in loop. 当我运行它时,它开始通过URL捕获文件,并循环循环。 After first getting file the terminal through was launched script requires to press button to continue. 首次获取文件后,终端通过启动脚本需要按按钮才能继续。 (I pressed Ctrl + C to start downloading next file). (我按Ctrl + C开始下载下一个文件)。

I don't understand the cause of this behavior, also I noticed script saved temporary files instead real. 我不了解此行为的原因,我也注意到脚本保存的是临时文件,而不是真实的。

Please, look at screenshot: 请看截图: 在此处输入图片说明

I assume this is due with operation system. 我认为这与操作系统有关。

I think the CTRL+C is necessary because of the line 我认为CTRL + C是必要的,因为该行

ERRORS.append(str(e))

What is exactly the variable 'ERRORS' ? 变量“ ERRORS”到底是什么? I think it's the cause of the freezing behavior problem. 我认为这是冻结行为问题的原因。 Try the code without ERRORS, and tell me if it works better. 尝试没有错误的代码,并告诉我它是否更好。

Another thing, I think that it would be better to use self.LOADED_IMAGES instead of LOADED_IMAGES. 另一件事,我认为使用self.LOADED_IMAGES而不是LOADED_IMAGES会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM