所以目前我试图让一个功能正确。基本上我试图做一个多路(并发)线程,将Json文件中的信息带入程序,然后为每个线程使用每个json对象,然后用这些信息执行代码。
到目前为止我所做的 - 此代码仅适用于确实有效的Multiprocess。:
#Read json File
with open('profileMulti.json', 'r', encoding='UTF-8') as json_data:
profiles_string = json.load(json_data)
def get_individual_profiles(config):
top_layer = config.get('Profiles')
if top_layer:
top_level_keys = ['profile_{}'.format(i) for i in range(len(top_layer))]
print(top_level_keys)
return [(key, top_layer.get(key)) for key in top_level_keys]
return []
def stringify(key, next_layer):
return [
' '.join(key.capitalize().split('_')),
next_layer.get('Name'),
next_layer.get('Last_Name'),
next_layer.get('Email'),
next_layer.get('Phone')
#etc etc...
]
config = profiles_string
profiles = get_individual_profiles(config)
pool = ThreadPool()
# Launch a process for each item
threads = [pool.apply_async(stringify, tuple(item)) for item in profiles]
# get() the results as each finishes
results = [res.get() for res in threads]
print('threaded results:')
for item in results:
print(item)
输出:
threaded results:
['Profile 0', 'Thrill', 'Ofit', 'Stack@hotmail.com', '123 412 123']
['Profile 1', 'Hellow', 'World', 'Stac321k@hotmail.com', '543 412 312']
哪个好。但问题是:
无论何时我想使用这些信息
为例
def checkoutNames(NameUrl, nameID):
payload = {
"shared": {
"challenge": {
"Name": item["Name"],
"Last_Name": item["Last_Name"],
"Email": item["Email"],
"Phone": item["Phone"],
它是否会识别这些属性,但我需要做的是我需要通过项1来调用它们..项目[2]等来自def stringify(key, next_layer):
的{{1}}现在还不知道怎么做,所以我不需要这样做。
另一个问题是每当我们说ETC时我在代码中使用了项1。然后它将只使用最后一个线程并跳过其余线程。所以如果我这样做
print(item[1])
然后唯一的输出是最后一个 Hello
所以我需要解决的问题:
1。使每个线程同时执行并使用这些信息执行代码
2。进行修复,以便我不需要使用项目1,而是使用项目['名称']。
所以问题是,这有可能吗?这个想法是什么?
编辑 - 这是我目前只有一个Json配置文件的代码,只有一个配置文件可以正常工作。这是没有多处理的
Json文件
{
"Profiles": {
"profile_0": {
"Url": "Myownwebsite.se",
"My-Note": "Helloworld",
"Email": "Stackoverflow@gmail.com"
"PersonNumber": "1234543",
"postal_code": "54123",
"given_name": "World",
"Last_name": "Hellow",
"street_address": "helloworld 123",
"city": "Stockholm",
"country": "Sweden",
"phone": "123456789",
"Color": "Red",
"house_number": "123",
"year": "2017"
},
"profile_1": {
"Url": "Myasdwfaesite.se",
"My-Note": "aasfase",
"Email": "fasfsef@gmail.com"
"PersonNumber": "5634543",
"postal_code": "123445",
"given_name": "Balling",
"Last_name": "Calling",
"street_address": "qwertr 123",
"city": "London",
"country": "UK",
"phone": "65412331",
"Color": "Blue",
"house_number": "321",
"year": "2018"
}
#Profile_2 etc etc
}
}
代码
with open('profileMulti.json', 'r', encoding='UTF-8') as json_data:
config = json.load(json_data)
NameUrl = config["Url"]
myNote = config["My-Note"]
def checkoutNames(NameUrl, nameID):
#Request & other codes - Removed to recude the code
#......
#......
headers = {
'Referer': '',
'Content-Type': ''
}
payload = {
"shared": {
"challenge": {
"email": config["Email"],
"PersonNumber": config["PersonNumber"],
"postal_code": config["ZipCode"],
"given_name": config["Name"],
"Last_name": config["LastName"],
"street_address": config["Address"],
"postal_code": config["ZipCode"],
"city": config["City"],
"country": config["Country"],
"email": config["Email"],
"phone": config["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["Color"],
"house_number": config["houseNumber"],
"year": config["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()
编辑
我只想打印出Yaroslav代码为输出提供的内容
答案 0 :(得分:1)
您制作列表并将其作为字典工作。如果订单很重要 - 请使用集合中的OrderedDict。顺便说一下,你可以用这样的东西加载json:
from collections import OrderedDict
.... # your previous code
profiles_string = json.load(json_data, object_pairs_hook=OrderedDict)
尝试使用此代码:
import json
from collections import OrderedDict
from multiprocessing.pool import ThreadPool
import threading
from time import sleep
import random
with open('profileMulti.json', 'r', encoding='UTF-8') as json_data:
config = json.loads(json_data, object_pairs_hook=OrderedDict)
def stringify(key, next_layer):
sleep(random.random()) # work emulation
# `key` is profile name (profile_0, profile_1 etc)
# `next_layer` is profile payload as OrderedDict:
# {
# "Url": "Myasdwfaesite.se",
# "My-Note": "aasfase",
# "Email": "fasfsef@gmail.com",
# "PersonNumber": "5634543",
# "postal_code": "123445",
# "given_name": "Balling",
# "Last_name": "Calling",
# "street_address": "qwertr 123",
# "city": "London",
# "country": "UK",
# "phone": "65412331",
# "Color": "Blue",
# "house_number": "321",
# "year": "2018"
#}
# this print statement just for illustartion
print(threading.current_thread().name, key)
return { key: next_layer } # still ordered
# how much workers should be started
profiles_number = len(config.get('Profiles', {}).items())
pool = ThreadPool(profiles_number)
# Launch a thread for each item and get() the results as each finishes
# `starmap_async` returns result object when all tasks are finished, so
# single get is called. Result object in thios case behave like list
results = pool.starmap_async(stringify, config.get('Profiles', {}).items()).get()
print('threaded results:')
for i in results:
print(i, end='\n***\n') # result separator when printed