我正在嘗試在 python 中使用執行緒,我對執行緒很陌生。我希望執行緒從同一個檔案中讀取隨機行,但所有執行緒都讀取同一行。所以我試圖閱讀的檔案的所有行都是 email:pass:another_line 格式。我希望用多個執行緒從同一個檔案中讀取不同的行,但它用多個執行緒讀取同一行。因此,例如 1 個執行緒將回傳 line1,第二個執行緒將回傳 line2 但它的隨機行。
import random
import threading
def email_pass_token():
global email, pass2, token
file = open("testing/pokens.csv").read().splitlines()
acc_str = random.choice(file)
num_lines = sum(1 for _ in file)
print(num_lines)
email = ":".join(acc_str.split(":", 1)[:1])
pass2 = ":".join(acc_str.split(":", 2)[:2][1:])
token = ":".join(acc_str.split(":", 3)[:3][2:])
email_pass_token()
def gen_acc():
print(email, pass2, token)
threads = []
num_thread = input("Threads: ")
num_thread = int(num_thread)
for i in range(num_thread):
t = threading.Thread(target=gen_acc)
threads.append(t)
t.start()
檔案示例:
[email protected]:#354946345e696$e30*417:another_line1
[email protected]:2e5548c543709!8@305-8(:another_line2
[email protected]:41c!954=7543cc^1#48fd_$*b5:another_line3
[email protected]:1f@e54d78^feb54355&6$50:another_line4
[email protected]:#3946345e696$e30*417:another_line5
[email protected]:2e58c5437709!8@305-8(:another_line6
[email protected]:41c!9=7543cc^1#48fd_$*b5:another_line7
[email protected]:1f@ed78^feb53455&6$50:another_line8
uj5u.com熱心網友回復:
您沒有運行用于在執行緒中選擇行的功能。您正在做的是gen_acc在僅列印三個變數的執行緒中運行email,pass2并且token. 您用于選擇行的代碼駐留email_pass_token在僅呼叫一次。
uj5u.com熱心網友回復:
這是您可以做到的一種方法。請注意,這將不若作業密碼包含一個冒號。另外,我已經對執行緒數進行了硬編碼,但您明白了。
import random
from concurrent.futures import ThreadPoolExecutor
FILENAME = 'testing/pokens.csv'
NTHREADS = 5
def myfunc():
with open(FILENAME) as infile:
lines = infile.readlines()
line = random.choice(lines).strip()
tokens = line.split(':')
return ' '.join(tokens)
def main():
with ThreadPoolExecutor() as executor:
futures = []
printable = set()
for _ in range(NTHREADS):
future = executor.submit(myfunc)
futures.append(future)
for future in futures:
printable.add(future.result())
for p in printable:
print(p)
if __name__ == '__main__':
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371845.html
標籤:Python 蟒蛇-3.x 多线程 线 python-多线程
