所以我现在正在玩线程,我已经到了线程确实可以运行但我认为我有for循环的问题。因此for循环只在代码内部工作,但每当我希望它运行整个代码时。然后它将只占用Json文件中的最后一个对象。
代码
#Read json File
with open('config.json', 'r', encoding='UTF-8') as json_data:
config = json.load(json_data)
threads = []
for i in range(len(config)):
p = threading.Thread(args=(config[i]))
threads.append(p)
p.start()
print()
print(config[i])
NameUrl = config[i]["Url"]
myNote = config[i]["My-Note"]
def checkoutNames(NameUrl, nameID):
#Request & other codes - Removed to recude the code
#......
#......
headers = {
'Referer': '',
'Content-Type': ''
}
payload = {
"shared": {
"challenge": {
"email": config[i]["Email"],
"PersonNumber": config[i]["PersonNumber"],
"postal_code": config[i]["ZipCode"],
"given_name": config[i]["Name"],
"Last_name": config[i]["LastName"],
"street_address": config[i]["Address"],
"postal_code": config[i]["ZipCode"],
"city": config[i]["City"],
"country": config[i]["Country"],
"email": config[i]["Email"],
"phone": config[i]["Phone"],
}
def checkoutNotes(NamesUrl, NamesPost):
#Request & other codes - Removed to recude the code
#......
#......
headers = {
'Accept': 'application/json, text/javascript, /; q=0.01',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': NameUrl,
'Connection': 'keep-alive'
}
payloadInfo = {
"Information": {
"Color": config[i]["Color"],
"house_number": config[i]["houseNumber"],
"year": config[i]["Year"]
}
}
def wipe():
os.system('cls' if os.name == 'nt' else 'clear')
def main():
time.sleep(1)
FindName(myNote)
if _name_ == '_main_':
try: {
main()
}
except KeyboardInterrupt:
wipe()
因此,您可以看到代码在开头有一个帖子,但我不知道为什么它没有通过整个代码?也许有人看到了这个问题吗?
这就是问题
threads = []
for i in range(len(config)):
p = threading.Thread(args=(config[i]))
threads.append(p)
p.start()
print()
print(config[i])
答案 0 :(得分:1)
你应该给线程一个目标,如果没有使用多线程没有意义。
#p = threading.Thread(target=function_name, args=(config[i]))
#try something like this
def yourfucntion(config):
#do your thing
pass
if __name__ =='__main__':
with open('config.json', 'r', encoding='UTF-8') as json_data:
config = json.load(json_data)
threads=[]
for i,e in enum(config):
threads.append(threading.Thread(target=yourfunction,args=(config[i] or e))