简体   繁体   中英

Use While Loop twice with an else statement

I couldnt find something similar to my question.

I'm currently working on a Thread threader function.

I have two queues, one r_queue for regular and p_queue for priority.

def _threader(self):
    # Thread Handler
    while self.active:
        while self.p_queue.empty() and self.r_queue.qsize() > 0:
            # Regular Queue
            queue_dict = self.r_queue.get()
            self._complete_task(queue_dict, prioritized=False)

        (else?) while self.p_queue.qsize() > 0:
            # Prioritized Queue
            queue_dict = self.p_queue.get()
            self._complete_task(queue_dict, prioritized=True)

How can I use the (else?) properly here?

Edit: The idea behind the threader is, that it receives the data from the queue and completes the function. The first while is that it priorities the priority queue and only calls in case it has a size. (Cant get something when its None existent) My problem here is, that I need to add a while loop for the priority queue the line with (else?) while self.p_queue.qsize() > 0: but I cant just use straight else. If i would, it would constantly pick "Nothing" if the queue size is None existent. Something like elif while self.p_queue.qsize() > 0 but that doesnt exist.

You do not need the inner while loops as you can go with:

def _threader(self):
    # Thread Handler
    while self.active:
        if self.p_queue.qsize() > 0:
            # Prioritized Queue
            queue_dict = self.p_queue.get()
            self._complete_task(queue_dict, prioritized=True)
        elif self.r_queue.qsize() > 0:
            # Regular Queue
            queue_dict = self.r_queue.get()
            self._complete_task(queue_dict, prioritized=False)

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