问题描述
尝试使用global从异步函数中删除Python中的Global数组s3_events中的记录。我对全球性的理解尚不清楚。 我的全局s3_events仍显示send1函数中的记录,这些记录已在异步函数send_to中删除。 看到了-Modify global variable from async function in python是否有其他模式或数据结构可以做到这一点?
TIA,
class IngestData(Kinesis):
def __init__(self,kds_name,kds_shards,cores,s3_bucket,s3_bucket_path,hr_arr,batch_size):
hr_arr = hr_arr
...
def collect_keys_each_hr(self):
for hr in self.hr_arr:
sec_dist = self.collect_keys(hr)
total_hr_per_sec_s3_files,total_hr_per_sec_records = self.send1(sec_dist)
def send1(self,sec_dist):
global s3_events # added to make it global
pool = mp.Pool()
for secKey in sec_dist:
# keeps adding recs to s3_events
# while the parallel_fn processes records from s3_events and removes the processed events from s3_events
s3_events_1s = read events into this array
s3_events = s3_events + s3_events_1s
parallel_fn(s3_events,pool)
pool.close()
def parallel_fn(self,s3_recs,pool):
# calc from_idx and to_idx
result = pool.apply_async(send_to,args=(from_idx,to_idx,s3_recs)) #async call to send1
results.append(result)
# send1 defined globally and not in the IngestData class
def send_to(from_idx,s3_recs):
global s3_events # not sure how to use global s3_events from the main section
s3_events = s3_recs #???
recs_already_sent = []
for s3_obj_rec in s3_recs[from_idx:to_idx]: # potential issue with indexes and removing records from the s3_recs
if (boolean logic true):
recs_already_sent.append(s3_obj_rec)
.....
for sent_rec in recs_already_sent:
s3_recs.remove(sent_rec) # remove from global s3_events
if __name__ == "__main__":
s3_events = []
s3_bucket_path = deployment_id + "/" + game_id + "/application-metrics/"
events = IngestData(kds_name,int(cores),bucket,batch_size)
events.collect_keys_each_hr()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)