Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # solo per togliermi uno sfizio sui dizionari...
- from random import randint, seed
- seed()
- dizio = dict()
- clSer = dict()
- # setta sotto per la dimensione del dizio
- # e per il numero dei thread che vuoi provare
- numeroClienti = 1000
- quanteVolte = 1000000
- class Clienti():
- def __init__(self, codUs):
- self.codUs = codUs
- self.lock = False
- def blocca(self):
- if self.lock:
- return False
- self.lock = True
- return True
- def sblocca(self):
- self.lock = False
- def carica():
- while True:
- codUs = chr(
- randint(2048,55295)).encode() + chr(
- randint(2048,55295)).encode() + chr(
- randint(2048,55295)).encode() + chr(
- randint(2048,55295)).encode()
- if not codUs in clSer:
- break
- return codUs
- import threading
- from queue import Queue
- import time
- ## lock to serialize console output
- #lock = threading.Lock()
- def do_work(item):
- item, chefaccio = item
- #if item not in dizio:
- #dizio[item] = Clienti(item)
- #dizio[item].codUs = carica()
- #clSer[dizio[item].codUs] = item
- #dizio[item].sblocca()
- ##print('carico ', item, ' con', dizio[item].codUs)
- if chefaccio < 7:
- # riscrivo
- while True:
- a = False
- while dizio[item].blocca():
- if dizio[item].codUs in clSer:
- clSer.__delitem__(dizio[item].codUs)
- codUs = carica()
- clSer[codUs] = item
- dizio[item].codUs = codUs
- dizio[item].sblocca()
- a = True
- break
- if a:
- break
- return
- if chefaccio < 9:
- # cancello
- while True:
- a = False
- while dizio[item].blocca():
- if dizio[item].codUs in clSer:
- clSer.__delitem__(dizio[item].codUs)
- dizio[item].codUs = None
- dizio[item].sblocca()
- a = True
- break
- if a:
- break
- return
- while True:
- # cazzeggio
- a = False
- while dizio[item].blocca():
- palle = dizio[item].codUs
- time.sleep(randint(1, 2)/10)
- if dizio[item].codUs != palle:
- print('differenza in', item)
- dizio[item].sblocca()
- a = True
- break
- if a:
- break
- # The worker thread pulls an item from the queue and processes it
- def worker():
- while True:
- item = q.get()
- do_work(item)
- q.task_done()
- # Create the queue and thread pool.
- q = Queue()
- for i in range(4):
- t = threading.Thread(target=worker)
- t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
- t.start()
- # stuff work items on the queue (in this case, just a number).
- start = time.perf_counter()
- passo = 0
- pip = 0
- smetti = False
- for j in range(numeroClienti +1):
- # carico qui
- dizio[j] = Clienti(carica())
- clSer[dizio[j].codUs] = j
- for volta in range(quanteVolte):
- passo += 1
- if passo == 1000:
- pip +=1
- passo = 0
- print('++++', pip)
- chi = randint(0, numeroClienti)
- cheFaccio = randint(0, 9)
- q.put((chi, cheFaccio))
- q.join() # block until all tasks are done
- print('time:',time.perf_counter() - start)
- print('len dizio', len(dizio), ' len clUser', len(clSer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement