I
I
ismail_20072021-03-23 10:41:53
Python
ismail_2007, 2021-03-23 10:41:53

How to run two functions in parallel in a given class in parallel?

I'm trying to run two functions of this class at the same time, so that one of them (get_mail_func( )), runs in 1 second intervals indefinitely, and the other one runs once (init_dialog( )). However, an error pops up: "imaplib.IMAP4.error: command LOGIN illegal in state AUTH, only allowed in states NONAUTH". If translated, it turns out that I am trying to log in again, which is why the error occurs. How to fix it?

class DialogMenu(tk.Toplevel):
    def __init__(self,user_name):
        super().__init__(root)
        self.user_name = user_name
        self.db = db
        self.get_mail_class_start = get_mail_class_start
        self.db.cur = self.db.conn.cursor()
        self.send_mail = Mail_class(user_name, smtplib.SMTP('smtp.gmail.com:587'))
        self.dialog_thread = threading.Thread(target = self.menu_thread(), args = ())
        self.dialog_thread.start()
 
    def retrieve_message_input(self):
        self.messsage_text = self.Message_Text.get(1.0, tk.END)
        self.send_mail.send_mail(self.messsage_text)
 
    def init_dialog(self):
        
        self.geometry('600x550')
        self.resizable(False,False)
        self['bg'] = '#2A3132'
        self.minsize(width = 900, height = 550) 
        self.title(f'Диалог с {self.user_name}')
 
        server = 'smtp.gmail.com:587'
        self.send_mail.start_client(server)
        
        self.Send_btn = tk.Button(self,text = '',padx=15,pady=12.5,bd=5,font=('Ubuntu',35),
                             bg = '#336B87',command = partial(self.dialog_db,self.user_name))
 
        self.Send_btn_sms = tk.Button(self,text = '➤',padx = 15,pady = 13,font = ('Ubuntu',35),bd = 5,
                                      bg = '#336B87', command = self.retrieve_message_input)
 
        self.Message_Text = tk.Text(self,width = 45,height = 5,bg='white',fg='black',font = 50)
        self.frame = tk.Frame(self)
        self.text = tk.Text(self.frame, width=85, height=22)
        self.scroll = tk.Scrollbar(self.frame, command=self.text.yview)
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.text.config(yscrollcommand=self.scroll.set)
        self.text.insert(tk.INSERT,("text" + '\n') * 50)
        self.text.configure(state='disabled')
 
        self.text.pack(side=tk.TOP) 
        self.text.place()
 
        self.frame.pack(side=tk.TOP)
        self.Send_btn.pack()
        self.Send_btn_sms.pack()
        self.Message_Text.pack()
 
        self.Send_btn.place(x = 670,y = 400)
        self.Send_btn_sms.place(x = 545, y = 400)
        self.Message_Text.place(x = 34,y = 400)
 
    def dialog_db(self, user_name):
        DialogDB(user_name)

    def menu_thread(self):
        self.init_dialog()
        while True:
            time.sleep(1)
            get_mail_class_start.get_mail_func()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-03-23
@ismail_2007

For tkinter loops, there is an after that won't break the loop of the program itself.
Example https://qna.habr.com/answer?answer_id=1897041

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question