# dataGen 增加 multiprocessing 函數 問題整理
## random 函數的問題
- fullprocess
```python
def full_process(n):
id = os.getpid()
if not os.path.exists(f'{id}'):
os.system(f'cp -r OpenFOAM {id}')
print("%s: Start the task" %(time.strftime('%X')))
print("Run {}:".format(n))
fileNumber = np.random.randint(0, len(files))
basename = os.path.splitext( os.path.basename(files[fileNumber]) )[0]
print("\tusing {}".format(files[fileNumber]))
length = freestream_length * np.random.uniform(1.,freestream_length_factor)
angle = np.random.uniform(-freestream_angle, freestream_angle)
fsX = math.cos(angle) * length
fsY = -math.sin(angle) * length
print("\tUsing len %5.3f angle %+5.3f " %( length,angle ) )
print("\tResulting freestream vel x,y: {},{}".format(fsX,fsY))
os.chdir(f'{id}')
if genMesh("../" + airfoil_database + files[fileNumber]) != 0:
print("\tmesh generation failed, aborting")
os.chdir("..")
exit(1)
runSim(fsX, fsY)
os.chdir("..")
n = int(n)
outputProcessing(basename, fsX, fsY, imageIndex=n)
print("%s: Done" %(time.strftime('%X')))
```
如果把產生 fsX, fsY 幾個函數的程式碼寫進迴圈中會因為平行運算的關係
使得用到的 random 函式中產生的亂數都會是相同的
- main
```python
if __name__=='__main__':
pool = mp.Pool(cpu_to_use)
startTime = time.time()
for n in range(samples):
r = pool.apply_async(full_process, args=(n, ))
#r.wait()
pool.close()
pool.join()
totalTime = (time.time() - startTime)/60
print(f'Final time elapsed: {totalTime:.2f} minutes')
```
- Output

## genMesh runSim postprocessing 不會執行
- 問題
可能是 apply_async 的 arguments 問題
- code
```python
def full_process(basename, fsX, fsY, n):
id = os.getpid()
if not os.path.exists(f'{id}'):
os.system(f'cp -r OpenFOAM {id}')
print("%s: Start the task" %(time.strftime('%X')))
print("Run {}:".format(n))
os.chdir(f'{id}')
if genMesh("../" + airfoil_database + files[fileNumber]) != 0:
print("\tmesh generation failed, aborting")
os.chdir("..")
exit(1)
status = runSim(fsX, fsY)
os.chdir("..")
n = int(n)
if status ==0:
outputProcessing(basename, fsX, fsY, id, imageIndex=n)
print("%s: Done" %(time.strftime('%X')))
return id
ids = []
def log_id(id):
if id == -1 : return
if id not in ids:
ids.append(id)
def main():
pool = mp.Pool(cpu_to_use)
startTime = time.time()
for n in range(samples):
fileNumber = np.random.randint(0, len(files))
basename = os.path.splitext( os.path.basename(files[fileNumber]) )[0]
print("\tusing {}".format(files[fileNumber]))
length = freestream_length * np.random.uniform(1.,freestream_length_factor)
angle = np.random.uniform(-freestream_angle, freestream_angle)
fsX = math.cos(angle) * length
fsY = -math.sin(angle) * length
print("\tUsing len %5.3f angle %+5.3f " %( length,angle ) )
print("\tResulting freestream vel x,y: {},{}".format(fsX,fsY))
pool.apply_async(full_process, args=(basename, fsX, fsY), callback=log_id)
pool.close()
pool.join()
totalTime = (time.time() - startTime)/60
print(f'Final time elapsed: {totalTime:.2f} minutes')
print(ids)
for id in ids:
os.system(f'rm -r {id}/')
if __name__=='__main__':
main()
```
- Output
