Not sure if it's possible within a pyt due to the nature of the geprocessing environment loading the pyt classes, the __main__ requirement. As a script tool- this would be the script named MultiProc.py and referenced (not embedded) in the script tool. The first Parameter is a value table, sting int.
The messages wont display from the thread until the threads are done so you can put them in the result dictionary to print them all.
import time
import arcpy
import multiprocessing as mp
import os
import sys
mp.set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))
def work_log(work_data):
res_dict = {'process': f"Process {work_data[0]} waiting {work_data[1]} seconds", 'result': ''}
time.sleep(work_data[1])
res_dict['result'] = f"Process {work_data[0]} Finished."
return res_dict
if __name__ == "__main__":
import Multiproc
work_table = arcpy.GetParameterAsText(0)
# Create a value table with 2 columns
value_table = arcpy.ValueTable(2)
# Set the values of the table with the contents of the first argument
if work_table:
value_table.loadFromString(arcpy.GetParameterAsText(0))
else:
for i in [["A", 5], ["B", 2], ["C", 1], ["D", 3]]:
value_table.addRow(i)
pairs = []
# Loop through the list of inputs
for i in range(0, value_table.rowCount):
pairs.append([value_table.getValue(i, 0), int(value_table.getValue(i, 1))])
arcpy.AddMessage(pairs)
with mp.Pool(2) as pool:
jobs = [pool.apply_async(Multiproc.work_log, (pair, )) for pair in pairs]
res = [j.get() for j in jobs]
for r in res:
arcpy.AddMessage(f"{r['process']} : {r['result']}")