[問題] 想詢問有沒有方法在指定行印出

作者: Risha5566 (理沙)   2021-02-24 00:48:05
大家安安
我的問題有點奇怪,請見諒QQ
我的問題大概是這樣
因為我有用Threading
每個job都會印出
print(f"({T}) Connecting {id1} ... ", end="\r")
print(f"({T}) Searching {id1} ... ", end="\r")
print(f"({T}) Downloading {id1} ... ", end="\r")
但是因為我有Threading
所以這些會擠在同一行
如果換行的話會變很亂
(7) Searching ANN22860-RA ...
(6) Searching ANN22863-RA ...
(15) Downloading ANN22865-RA ...
(14) Downloading ANN22859-RA ...
(2) Downloading ANN22873-RA ...
(19) Downloading ANN24587-RA ...
(13) Searching ANN22875-RA ...
(17) Downloading ANN22871-RA ...
(3) Downloading ANN22861-RA ...
(16) Downloading ANN22866-RA ...
像這樣
而且每個Thread的執行時間不太一樣
請問有沒有辦法可以讓
第一個Thread就印在terminal上的第一行
第二個Thread就印在terminal上的第二行
希望是可以印出
terminal上第一行 (1) Connecting {id1} ...
terminal上第二行 (2) Searching {id2} ...
. (3) Connecting {id3} ...
. (4) Downloading {id4} ...
. (5) Finish
如果看不懂我的問題的話
我會再努力解釋的!!
先謝謝大家了!!
執行環境:
python 3.6 upup
linux
作者: AndCycle (AndCycle)   2021-02-24 03:33:00
你只能自己先把想印的東西用別的東西接起來排好輸出要有一個輸出專用的thread, 或是反過來去定時讀狀態輸出
作者: ckc1ark (偽物)   2021-02-24 04:08:00
curses比較像是你要的 一樣要有輸出專用的daemon thread
作者: remia81   2021-02-24 04:17:00
pbar = tqdm(T_job_input_list ,desc=f"" , bar_format="{desc}", position=T, ncols=80, miniters=1)for j in pbar:id1 = connect(j)pbar.set_description(f"({T}) Connecting {id1}")search(id1, j)pbar.set_description(f"({T}) Searching {id1})
作者: lycantrope (阿寬)   2021-02-25 14:29:00
獨立thread用sys.stdout.write輸出,用lock讓輸出排序
作者: leolarrel (真.粽子無雙)   2021-02-25 16:46:00
我也推薦用curses處理
作者: Risha5566 (理沙)   2021-03-03 00:12:00
感謝~~

Links booklink

Contact Us: admin [ a t ] ucptt.com