2015-10-30 41 views
0

我正在嘗試製作將數據打印到文件中的池。由多個池工作人員打印到文件

def get_and_print_something(url): 

    with open('file.txt','a') as f: 
     f.write(get_line(url)) 

pool = Pool(50) 

for url in urls: 
    pool.apply_async(get_something, args=(url,)) 

問題是有時它寫錯了數據。這是因爲兩名工人在同一時間使用相同的文件進行操作。是否有可能允許等待文件可以修改?的TXT的

實施例:

This is a correct line. 
This is a correct line. 
orrect line. 
This is a correct line. 
... 
+0

使用適當的同步機制,見https://docs.python.org/3/library /threading.html#lock-objects。 – JimmyB

回答

0

可以從例如採取的示例這個網站:

http://effbot.org/zone/thread-synchronization.htm#locks,或

https://pymotw.com/2/threading/

這基本上可以歸結爲:

import threading 

lock = threading.Lock() 

def get_and_print_something(url): 

    # Not yet in critical section because we want this to happen concurrently: 
    line = get_line(url) 

    lock.acquire() # Will wait if necessary until any other thread has finished its file access. 

    # In critical section now. Only one thread may run this at any one time. 

    try: 
     with open('file.txt','a') as f: 
      f.write(line) 
    finally: 
     lock.release() # Release lock, so that other threads can access the file again. 
相關問題