import multiprocessing as mp
import threading
processingP = mp.Process(target=a_process, args=(processCollection,mongoIP, mongoPort))
processingP.start()
def createSigneIndexForCollection(mongoDatabase, collectionName, columnName):
rawlogCollection = mongoDatabase[collectionName]
rawlogCollection.create_index([(columnName , 1)])
def createIndexForCollection(mongoDatabase, collectionName):
columnsToBeIndexed = ["Name", "Message",etc.]
for columnName in columnsToBeIndexed:
indexThread = threading.Thread(target=createSigneIndexForCollection, args=(mongoDatabase, collectionName, columnName))
indexThread.start()
indexThread.join()
def a_process(processCollection, mongoIP, mongoPort):
mongoDatabase = getDatabase(mongoIP, mongoPort, mongoDatabase)
createIndexForCollection(mongoDatabase, processCollection)
if processCollection != "NULL":
ScanningThread = threading.Thread(target=check, args=(mongoDatabase, processCollection))
ScanningThread .start()
ScanningThread .join()
在上面的代码片段中,每个进程将每隔6分钟启动一次。每个进程将访问createIndexForCollection函数并转到下一行。但我不希望新创建的进程在当前进程访问createIndexForCollection函数时访问该函数。一旦当前进程在createIndexForCollection函数上执行完毕,则只有下一个进程才应访问创建用于集合的索引函数
1条答案
按热度按时间raogr8fs1#
您可以使用跨进程共享的令牌和条件变量来序列化进程,如下所示:
结果:
祝你好运!