Skip to content

Instantly share code, notes, and snippets.

@kingmo888
Last active May 9, 2017 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingmo888/a7cdc36f77397f65ac19fb49f5795814 to your computer and use it in GitHub Desktop.
Save kingmo888/a7cdc36f77397f65ac19fb49f5795814 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import pandas as pd
import multiprocessing
import numpy as np
import time
import os
print('main...') # 这个输出为何会打印了N次?
def func(f, lock, i):
sleeptime = round(np.random.rand() / 10, 4)
time.sleep(sleeptime)
print(i)
lock.acquire()
try:
try:
f.writelines('num:{0}, sleeptime[{1}]\n'.format(i, sleeptime))
except Exception as e:
print(e)
#f.close()
finally:
lock.release()
if __name__ == '__main__':
try:
os.remove('test.txt')
except:
pass
f = open('test.txt', 'a')
lock = multiprocessing.Manager().Lock()
print('begin...')
#lock = multiprocessing.Lock()
pool = multiprocessing.Pool(processes=3)
for i in range(10):
pool.apply_async(func, (f, lock, i))
pool.close()
pool.join()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment