简体   繁体   中英

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.

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. Let me explain more.

When I run it it begins to catch files by URL, iterating in loop. 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).

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

ERRORS.append(str(e))

What is exactly the variable '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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