Uczyń losowy moduł bezpiecznym wątkiem w Pythonie
Mam aplikację wymagającą tych samych wyników, z tym samym losowym materiałem siewnym. Ale znalazłem random.randint bez wątków. Próbowałem mutex, ale to nie działa. Oto mój kod eksperymentu (długi, ale prosty):
import threading
import random
def child(n, a):
g_mutex = threading.Lock()
g_mutex.acquire()
random.seed(n)
for i in xrange(100):
a.append(random.randint(0, 1000))
g_mutex.release()
def main():
a = []
b = []
c1 = threading.Thread(target = child, args = (10, a))
c2 = threading.Thread(target = child, args = (20, b))
c1.start()
c2.start()
c1.join()
c2.join()
c = []
d = []
c1 = threading.Thread(target = child, args = (10, c))
c2 = threading.Thread(target = child, args = (20, d))
c1.start()
c1.join()
c2.start()
c2.join()
print a == c, b == d
if __name__ == "__main__":
main()
Chcę kodować, aby drukowaćPrawda, prawda, ale ma szansę daćfalse, false. Jak mogę zrobić randint z ochroną wątków?