Last active
May 9, 2017 14:21
-
-
Save kingmo888/a7cdc36f77397f65ac19fb49f5795814 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
# -*- 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