Advertisement
Guest User

prova dict pin python

a guest
Feb 16th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1.  
  2. # solo per togliermi uno sfizio sui dizionari...
  3.  
  4. from random import randint, seed
  5. seed()
  6.  
  7.  
  8. dizio = dict()
  9. clSer = dict()
  10. # setta sotto per la dimensione del dizio
  11. # e per il numero dei thread che vuoi provare
  12. numeroClienti = 1000
  13. quanteVolte = 1000000
  14.  
  15. class Clienti():
  16.  
  17.     def __init__(self, codUs):
  18.         self.codUs = codUs
  19.         self.lock = False
  20.     def blocca(self):
  21.         if self.lock:
  22.             return False
  23.         self.lock = True
  24.         return True
  25.     def sblocca(self):
  26.         self.lock = False
  27.  
  28. def carica():
  29.     while True:
  30.         codUs = chr(
  31.         randint(2048,55295)).encode() + chr(
  32.         randint(2048,55295)).encode() + chr(
  33.         randint(2048,55295)).encode() + chr(
  34.         randint(2048,55295)).encode()
  35.         if not codUs in clSer:
  36.             break
  37.     return codUs
  38.  
  39. import threading
  40. from queue import Queue
  41. import time
  42.  
  43. ## lock to serialize console output
  44. #lock = threading.Lock()
  45.  
  46. def do_work(item):
  47.  
  48.     item, chefaccio = item
  49.     #if item not in dizio:
  50.         #dizio[item] = Clienti(item)
  51.         #dizio[item].codUs = carica()
  52.         #clSer[dizio[item].codUs] = item
  53.         #dizio[item].sblocca()
  54.         ##print('carico ', item,  ' con', dizio[item].codUs)
  55.     if chefaccio < 7:
  56.         # riscrivo
  57.         while True:
  58.             a = False
  59.             while dizio[item].blocca():
  60.                 if dizio[item].codUs in clSer:
  61.                     clSer.__delitem__(dizio[item].codUs)
  62.                 codUs = carica()
  63.                 clSer[codUs] = item
  64.                 dizio[item].codUs = codUs
  65.                 dizio[item].sblocca()
  66.                 a = True
  67.                 break
  68.             if a:
  69.                 break
  70.         return
  71.     if chefaccio < 9:
  72.         # cancello
  73.         while True:
  74.             a = False
  75.             while dizio[item].blocca():
  76.                 if dizio[item].codUs in clSer:
  77.                     clSer.__delitem__(dizio[item].codUs)
  78.                     dizio[item].codUs = None
  79.                 dizio[item].sblocca()
  80.                 a = True
  81.                 break
  82.             if a:
  83.                 break
  84.         return
  85.     while True:
  86.         # cazzeggio
  87.         a = False
  88.         while dizio[item].blocca():
  89.             palle = dizio[item].codUs
  90.             time.sleep(randint(1, 2)/10)
  91.             if dizio[item].codUs != palle:
  92.                 print('differenza in', item)
  93.             dizio[item].sblocca()
  94.             a = True
  95.             break
  96.         if a:
  97.             break
  98.  
  99.  
  100. # The worker thread pulls an item from the queue and processes it
  101. def worker():
  102.     while True:
  103.         item = q.get()
  104.         do_work(item)
  105.         q.task_done()
  106.  
  107. # Create the queue and thread pool.
  108. q = Queue()
  109. for i in range(4):
  110.      t = threading.Thread(target=worker)
  111.      t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
  112.      t.start()
  113.  
  114. # stuff work items on the queue (in this case, just a number).
  115. start = time.perf_counter()
  116. passo = 0
  117. pip = 0
  118. smetti = False
  119. for j in range(numeroClienti +1):
  120.     # carico qui
  121.     dizio[j] = Clienti(carica())
  122.     clSer[dizio[j].codUs] = j
  123.  
  124. for volta in range(quanteVolte):
  125.     passo += 1
  126.     if passo == 1000:
  127.         pip +=1
  128.         passo = 0
  129.         print('++++', pip)
  130.  
  131.     chi = randint(0, numeroClienti)
  132.     cheFaccio = randint(0, 9)
  133.     q.put((chi, cheFaccio))
  134.     q.join()       # block until all tasks are done
  135.  
  136. print('time:',time.perf_counter() - start)
  137. print('len dizio',  len(dizio),  '  len clUser',  len(clSer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement