나는 twisted와 함께 python으로 목록에 동시에 액세스 할 수있는 질문이 있습니다. 목록에 데이터를 추가하는 꼬인 클래스와 4 초마다 호출되는 메서드가 있습니다. 이 메소드는 목록의 요소를 처리하고 일부 작업을 수행합니다. ossPeriodic과 dataReceived 모두에서 같은 목록에 액세스하면 일관성 문제가 발생할 수 있습니다.Twisted 및 Python 목록 동시 액세스
ossStorage=[]
def ossPeriodic():
for i in ossStorage:
ossStorage.remove(i)
db.insertDataToDb(i)
reactor.callLater(4, ossPeriodic)
class OSS(Protocol):
def dataReceived(self, data):
account = pickle.loads(data)
ossStorage.append(account)
def main():
ossFactory = Factory()
ossFactory.protocol = OSS
reactor.listenTCP(50000, ossFactory)
reactor.callLater(4, ossPeriodic)
reactor.run()
내가 잠금 사용해야 또는 이와 유사한 : 여기 코드인가? 감사합니다.
아마도 큐를 사용해야 도움이 될까요? – asdf