Last active
May 9, 2017 02:39
-
-
Save kingmo888/be4d2dfa033cd50c7bca01db70196ee2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
f = open('test.txt', 'a') | |
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 | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment