[英]Threading python
我正在制作一個機器人,它解析輸入帳戶的訂閱者以登錄到企業帳戶類別和頭像的存在。 我想在四個線程中進行解析,但事實證明循環只運行了四次。 告訴我如何正確地做
import instaloader
import threading
L = instaloader.Instaloader()
L.login(login, password) # (log in)
nickname = str(input('Enter a nickname '))
# Checking followers without pic
followers_no_pic = []
# Checking the list of subscribers on existing business accounts
check_business = []
# Create list of followers
followers_list = []
print(f'Analysisof user {nickname}'
f'\nThe process may take some time, please wait')
# NICK FROM USER
Profile = instaloader.Profile.from_username(L.context, nickname)
def s(num):
for followers in Profile.get_followers():
followers_list.append(followers.username)
account = instaloader.Profile.from_username(L.context, followers.username)
if account.is_business_account is True:
check_business.append(account.is_business_account)
pic = account.profile_pic_url
pic = str(pic)
print('P r o c e s s')
if "https://instagram" in pic:
followers_no_pic.append(pic)
else:
pass
t1 = threading.Thread(target=s, args=(0.1,))
t2 = threading.Thread(target=s, args=(0.1,))
t3 = threading.Thread(target=s, args=(0.1,))
t4 = threading.Thread(target=s, args=(0.1,))
s(1)
t1.start()
t2.start()
t3.start()
t4.start()
print(f'User {nickname}'f'\nNumber of subscribers: {len(followers_list)}')
print(f'\nWithout pics: {len(followers_no_pic)}')
print(f'\nBusiness Accounts: {len(check_business)}')
# bot analytics: account name, number of subscribers, subscribers without avatars and business accounts
您可以使用 Threadpool 將本示例中的處理拆分為 4 個線程,以便您可以為其提供輸入列表和 function,它將並行運行 4 個線程,直到輸入列表中的所有項目都得到處理。
from multiprocessing.pool import ThreadPool
with ThreadPool(processes=4) as pool:
results = pool.map(s, [1, 2, 3, 4, 5])
print(results)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.