D
D
Daniil Lebedinsky2020-05-12 23:14:23
Python
Daniil Lebedinsky, 2020-05-12 23:14:23

How to stop the cycle?

Everyone shalom
There is some code written using the PyQt library ...

class whoLike(QtWidgets.QMainWindow, who_likes.Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.whoLiked)
        self.pushButton_2.clicked.connect(self.exitApp)
        self.listWidget.itemClicked.connect(self.selectionChanged)

    def selectionChanged(self, item):
        a = ("{}".format(item.text()))
        print(a)
        webbrowser.open(a)

    def exitApp(self):
        self.close()

    def whoLiked(self):
        self.listWidget.clear()

        response = requests.get(
            'https://api.vk.com/method/friends.get?access_token=%s&v=%s&order=name' % (access_token, version))
        data = response.json()
        print(data)

        data_fil = data['response']["items"]
        len_check = len(data_fil)
        print(data_fil)

        mass_likes = []
        i = 0
        check_x = 0

        for filter_data in data_fil:
            response_posts = requests.get('https://api.vk.com/method/wall.get?access_token=%s&v=%s&owner_id=%s' % (
            access_token, version, filter_data))
            data_posts = response_posts.json()

            check_x += 1
            self.label_5.setText(str(check_x) + '/' + str(len_check))
            i += 1

            if i >= 7:
                # Ждем 3 секунды
                loop = QEventLoop()
                QTimer.singleShot(3000, loop.quit)
                loop.exec()
                i = 0

            else:
                try:
                    post = data_posts['response']['items']
                    print(post)
                    for posts in post:
                        likes_posts = posts['likes']['user_likes']
                        if likes_posts == 1:
                            user_id = posts['owner_id']
                            id_post = posts['id']
                            url = 'https://vk.com/id%s?w=wall%s_%s' % (user_id, user_id, id_post)
                            mass_likes.append(url)
                            self.listWidget.addItem(url)
                            print(url)
                        else:
                            continue
                except:
                    print('User was deleted or banned')

There is a cycle "for filter_data in data_fil", it is necessary that by pressing the button "pushButton_2" the cycle stops. How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-05-12
@StellandYT

You have such a long cycle. what needs to be stopped with a button - redo it. This should not be in an application.
Long-running operations should be done in chunks and asynchronously.
Create a task queue. Each task should be small and atomic.
You have here, apparently, solid architectural noodles in the code.
It is not clear how you run this code, it is not clear what you have around it.
It is only clear that you have made some kind of ornate crutch here and are trying to solve a standard problem with some kind of handicraft dendrofecal method.
I can summarize everything.

  • Isolate and isolate the problem model.
  • Accumulate them in a queue.
  • Fill the queue when required. Pull and process individual tasks whenever possible, but asynchronously.

You can think in more detail if you explain what you are trying to achieve here.
If you try to play by your rules and give an answer to your not very correct question, then you need to process the interface in a separate thread, raise the mutex between threads and check its state in this cycle. But this is a bad way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question