forked from astericks/kodekombat12
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py~
More file actions
69 lines (50 loc) · 1.6 KB
/
server.py~
File metadata and controls
69 lines (50 loc) · 1.6 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
66
67
68
69
#!/usr/bin/python
import threading
import time
import sys
b1='U' #bot1s current move
b2='R' #bot2s current move
bot1_move_lock=threading.Lock() #lock to provide synchronisation to b1
bot2_move_lock=threading.Lock() #lock ot provide syncrhonisation to b2
exitFlag = 0
class thread_to_read_bot1_move_from_file (threading.Thread):
def __init__(self, threadID,filename):
self.threadID = threadID
threading.Thread.__init__(self)
def run(self):
global b1
print "Starting " + str(self.threadID)
bot1_move_lock.acquire()
print "Before" + b1
b1='L'
print "After" + b1
bot1_move_lock.release()
print "Exiting " + str(self.threadID)
class thread_to_read_bot2_move_from_file (threading.Thread):
def __init__(self, threadID,filename):
self.threadID = threadID
threading.Thread.__init__(self)
def run(self):
global b1
print "Starting " + str(self.threadID)
bot2_move_lock.acquire()
print "Before" + b1
b2='L'
print "After" + b1
bot2_move_lock.release()
print "Exiting " + str(self.threadID)
bot1_move_file=open("files/bot1_move","r")
bot2_move_file=open("files/bot2_move","r")
thread1 = thread_to_read_bot1_move_from_file(1,bot1_move_file)
thread2 = thread_to_read_bot2_move_from_file(1,bot2_move_file)
thread1.start()
thread2.start()
while(True):
if ( thread1.isAlive() or thread2.isAlive()):
continue
if( bot1_move_lock.acquire(False)):
print b1,b2
break
else:
pass
print "Exiting main thread"