如何在列表中添加每个并行进程的时间戳附加字典?
how to add the timestamp of each parallel process appending a dictionary in the list?
我有这样的代码:
import requests
import multiprocessing as mp
import json
import time
BASE_URL = 'http://127.0.0.1:3001/employees'
with open('data2.json', 'r') as f:
list_dict = json.load(f)
def resource_post(post_data):
stuff_got = []
response = requests.post(BASE_URL, json=post_data)
stuff_got.append(response.json())
print(stuff_got)
time.sleep(0.2)
return stuff_got
if __name__ == '__main__':
start=time.time()
with mp.Pool(processes=2) as pool:
pool.imap(resource_post, list_dict)
pool.close()
pool.join()
elapsed = (time.time() - start)
print("\n","time elapsed is :", elapsed)
文件data2.json列表中有几个字典没有时间戳,例如:
[{"TransID":123123,"User":"User1","ServiceID":62801238,"ProjID":"1-Proj"},{"TransID":123124,"User":"User1","ServiceID":62801238,"ProjID":"1-Proj"}]
在BASE_URL上有这样的数据:
{
"employees": [
{
"id": 1,
"TransID": "123122",
"User": "user1",
"timestamp": "20200224 12:33:33:334",
"ServiceID": "62801238",
"ProjID": "1-Proj"
}
]
}
并行处理后的预期输出并根据每个处理添加时间戳:
{
"employees": [
{
"id": 1,
"TransID": 123122,
"User": "user1",
"timestamp": "20200224 12:33:33:334",
"ServiceID": "62801238",
"ProjID": "1-Proj"
},
{
"TransID": 123123,
"User": "User1",
"timestamp": "20200310 9:20:33:334"
"ServiceID": 62801238,
"ProjID": "1-Proj",
"id": 2
},
{
"TransID": 123124,
"User": "User1",
"timestamp": "20200310 9:20:35:330"
"ServiceID": 62801238,
"ProjID": "1-Proj",
"id": 3
}
]
}
注意:时间戳将根据每个词典被处理并进入列表的时间添加到每个词典中。
所以我应该在我的代码中添加什么,以便每个进程的输出都有一个额外的时间戳。
请帮我。谢谢
您可以在 post_data
dict
中附加时间戳键和值
def resource_post(post_data):
stuff_got = []
post_data["timestamp"] = time.time() # Add the timestamp in the required format here
response = requests.post(BASE_URL, json=post_data)
stuff_got.append(response.json())
print(stuff_got)
time.sleep(0.2)
return stuff_got
我有这样的代码:
import requests
import multiprocessing as mp
import json
import time
BASE_URL = 'http://127.0.0.1:3001/employees'
with open('data2.json', 'r') as f:
list_dict = json.load(f)
def resource_post(post_data):
stuff_got = []
response = requests.post(BASE_URL, json=post_data)
stuff_got.append(response.json())
print(stuff_got)
time.sleep(0.2)
return stuff_got
if __name__ == '__main__':
start=time.time()
with mp.Pool(processes=2) as pool:
pool.imap(resource_post, list_dict)
pool.close()
pool.join()
elapsed = (time.time() - start)
print("\n","time elapsed is :", elapsed)
文件data2.json列表中有几个字典没有时间戳,例如:
[{"TransID":123123,"User":"User1","ServiceID":62801238,"ProjID":"1-Proj"},{"TransID":123124,"User":"User1","ServiceID":62801238,"ProjID":"1-Proj"}]
在BASE_URL上有这样的数据:
{
"employees": [
{
"id": 1,
"TransID": "123122",
"User": "user1",
"timestamp": "20200224 12:33:33:334",
"ServiceID": "62801238",
"ProjID": "1-Proj"
}
]
}
并行处理后的预期输出并根据每个处理添加时间戳:
{
"employees": [
{
"id": 1,
"TransID": 123122,
"User": "user1",
"timestamp": "20200224 12:33:33:334",
"ServiceID": "62801238",
"ProjID": "1-Proj"
},
{
"TransID": 123123,
"User": "User1",
"timestamp": "20200310 9:20:33:334"
"ServiceID": 62801238,
"ProjID": "1-Proj",
"id": 2
},
{
"TransID": 123124,
"User": "User1",
"timestamp": "20200310 9:20:35:330"
"ServiceID": 62801238,
"ProjID": "1-Proj",
"id": 3
}
]
}
注意:时间戳将根据每个词典被处理并进入列表的时间添加到每个词典中。
所以我应该在我的代码中添加什么,以便每个进程的输出都有一个额外的时间戳。 请帮我。谢谢
您可以在 post_data
dict
def resource_post(post_data):
stuff_got = []
post_data["timestamp"] = time.time() # Add the timestamp in the required format here
response = requests.post(BASE_URL, json=post_data)
stuff_got.append(response.json())
print(stuff_got)
time.sleep(0.2)
return stuff_got