如何在列表中添加附加字典的每个并行进程的时间戳?
Posted
技术标签:
【中文标题】如何在列表中添加附加字典的每个并行进程的时间戳?【英文标题】:how to add the timestamp of each parallel process appending a dictionary in the list? 【发布时间】:2021-06-08 00:28:40 【问题描述】:我有这样的代码:
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
]
注意:时间戳会根据每个字典被处理并进入列表的时间添加到每个字典中。
那么我应该在我的代码中添加什么,以便每个进程的输出都有一个额外的时间戳。 请帮我。谢谢
【问题讨论】:
【参考方案1】:您可以在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
【讨论】:
大家好,我已经尝试过你的方法,但是时间戳只在运行进程时出现在日志中,它没有发送到 db.json,是有什么问题还是我应该添加一些东西?跨度> 更新了上面的sn-p代码,请尝试 谢谢楼主,问题已经解决了:) @Irfan 如果您对答案感到满意,请接受答案,以便答案的作者获得应有的荣誉以上是关于如何在列表中添加附加字典的每个并行进程的时间戳?的主要内容,如果未能解决你的问题,请参考以下文章