正如我在標(biāo)題中所說,我想創(chuàng)建string variables使用. 并使用刪除它們。Threadsinputflag我需要一步步解釋這個問題。比方說,我input從用戶那里得到。這input由用戶將是 的名稱Thread variable。讓input等于love和like。在這種情況下,將創(chuàng)建 2 個線程變量和線程。他們的名字將是loveand like。要創(chuàng)建一個Thread,必須給出這樣的代碼。代碼:from threading import Threadimport timeimport re# Using while loop. Because I want to create multiple Threads by doing this.# Dictsdicts = {}flags = {}while True: # Input threadName = input('Thread name please? ') # To delete a Thread if 'delete' in threadName: delThread = re.search(r'delete (.*)', threadName) if delThread: delThread = list(map(str, delThread.groups())) delThread = ''.join(delThread) print('DELETING:', delThread) flags[delThread] = True print('DICT NOW:', flags) else: # Target function for every Thread. Print a message every 3 secs. def targetfunc(tname): while True: if flags[tname] in flags and flags[tname] == True: break print(f"I'm {tname} Thread.") time.sleep(3) # Create Threads. 'dicts[threadName]' will be whatever the user enters input. # Should be string variable. # 'threadName' is equal to input too. dicts[threadName] = Thread(target = targetfunc, args = [threadName]) dicts[threadName].start() flags[threadName] = False print(dicts) print(flags)我正在使用 2 個字典。一個dicts用于創(chuàng)建Threads,另一個用于使用刪除它們flag。要創(chuàng)建,只需鍵入您要調(diào)用的線程即可。要刪除,請鍵入delete (thread name)。KeyError當(dāng)我嘗試刪除時,此代碼會為每個線程拋出。這是完整的錯誤。這就是程序。如何解決這個問題?我想要實(shí)現(xiàn)的是:當(dāng)我鍵入名稱時,該名稱必須創(chuàng)建一個Thread具有該名稱的新名稱。但是當(dāng)我輸入時delete (thread name)應(yīng)該停止(thread name) Thread。我希望我能解釋清楚。希望你幫忙。
如何為線程創(chuàng)建字符串變量并使用標(biāo)志刪除它們
繁星點(diǎn)點(diǎn)滴滴
2023-06-20 14:36:49