I want to open 3 Powershell and run the same code at the 3 of them, just different files.
As they'll have exactly the same logic, each of them will try to access each other's files to check if there's anything written there
Process1 has client1.txt, Process2 has client2.txt and Process3 has client3.txt
Here's some code as to what process 3 should check before choosing which problem to work on:
import os
while True:
f = 'C:\Users\Files'
i = {}
i['word'] = 'problem X' #just an example, I'll have a whole list
if os.path.exists(f):
try:
os.rename(f, f)
print 'Access on file "' + f +'" is available!'
file1 = open('C:\Users\Files\client1.txt', 'r+')
file2 = open('C:\Users\Files\client2.txt', 'r+')
if file1.read() == i['word']:
print "A process is already working on this problem, check another"
elif file2.read() == i['word']:
print "A process is already working on this problem, check another"
else:
print "Found a new problem to work on"
file3 = open('C:\Users\Files\client3.txt', 'r+')
file3.write(i['word'])
file1.close()
file2.close()
file3.close()
except OSError as e:
print 'Access-error on file "' + f + '"! \n' + str(e)
time.sleep(5)
pass
What I tried to represent through the code is: I only want a process to start a problem if the others aren't working on it already, as they all have the same logic they'll try to solve the same problem (I have lots that need solving) and so they might reach at around the same time as the program goes on with the while True
.
When it finishes the problem, it'll delete the contents of the file and pick a new problem, then write the problem it is working at in its own file for the others to check later.
Just to make it a bit clear: Let's say process1 found a problem to work first ('AAA'), they all have empty txt files, it'll check txt2 and txt3 and see it's not equal to ('AAA'), then it'll write it to its own file and close it.
I want process2 which might make it there a second later to read both txt1 and txt3 and see that ('AAA') is already being worked on, it'll get the next in the list and check again, seeing that ('BBB') is okay and it'll write on its own file.
When it ends, it deletes the String from the txt and starts looking for another one.
There's the problem of both process trying to check files at the same time too. Maybe if there's a way to put a time.sleep()
to a process if another process is using the file and then try again a bit later?