-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.py
More file actions
65 lines (53 loc) · 1.77 KB
/
Tests.py
File metadata and controls
65 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Created on Feb 2, 2013
@author: kzaky
'''
import threading, re, Queue, time
class Peers(threading.Thread):
def __init__(self, delete_queue):
super(Peers, self).__init__()
self.delete_queue = delete_queue
self.stoprequest = threading.Event()
def run(self):
MyName = self.getName()
stop = 0
while (stop == 0):
if not self.delete_queue.empty():
DeletePeer = self.delete_queue.get()
if (DeletePeer != MyName):
self.delete_queue.put(DeletePeer)
else:
stop = 1
# here peer works on other stuff...
# print currently active peers (i.e., threads)
# 'MainThread' always included, but we do not show/count it
def PrintInfoCurrentPeers():
peer_count = threading.active_count()-1
print "Currently %d peers: " % peer_count
for p in threading.enumerate():
name = p.getName()
if (name != 'MainThread'):
print 'Peer:', name
# data structure containing peers to delete
delete_queue = Queue.Queue()
while 1:
execute_command = 1
PrintInfoCurrentPeers()
command = raw_input("Enter command: ")
words = command.split()
if (len(words) == 0):
execute_command = 0
elif (execute_command == 1 and words[0] == "addp"):
execute_command = 0
p = Peers(delete_queue)
p.setName(words[1])
p.start()
elif (execute_command == 1 and words[0] == "delp"):
execute_command = 0
delete_queue.put(words[1])
elif (execute_command == 1 and words[0] == "delallp"):
execute_command = 0
for p in threading.enumerate():
delete_queue.put(p.getName())
elif (execute_command == 1):
print "unknown command"