#!/usr/bin/python3 # -*- coding: utf-8 -*- # -------------------------------- # Name GrapHan_pin # Author DELL # Date 2020/9/22 # 多线程 #多线程处理图谱数据,把图谱中所有节点都更新拼音编码属性 # ------------------------------- import pinyin from py2neo import Graph from threading import Thread import time import math import random class Grap_pinyin: def __init__(self): self.neo_graph = Graph( "http://192.168.3.150:7480", username="neo4j", password="root" ) def getStrAllAplha(str): return pinyin.get_initial(str, delimiter="").upper() def getStrFirstAplha(str): str = getStrAllAplha(str) str = str[0:1] return str.upper() def processsGrep(neo_graph,df,data): for jj in data: try: query = 'match(d) where d.name=%r set d.拼音编码=%r ' % (jj, getStrAllAplha(jj)) print(df, query) neo_graph.run(query) except Exception as e: print(e) def split_def(): graph_object = Grap_pinyin() neo_graph = graph_object.neo_graph query = 'match(d) where d.拼音编码 is null return collect(distinct d.name) as data' data = neo_graph.run(query).data()[0]['data'] thread_list = [] # 每个线程处理的数据大小 split_count = 20 # 需要的线程个数 times = math.ceil(len(data) / split_count) count = 0 for item in range(times): _list = data[count:count+split_count] #线程相关处理 thread = Thread(target=processsGrep,args=(neo_graph,item,_list)) thread_list.append(thread) #在子线程中运行任务 thread.start() count += split_count #线程同步,等待子线程运行结束,主线程再结束 for _item in thread_list: _item.join() if __name__ == '__main__': split_def()