Reputation: 137
I am using robotframework. I am using pabot to run test suites in parallel.
While running I need to make sure that a file is accessible by one process at a time and other should wait for the process to release that file.
I understand that it could be done through multithreading in python. I am newbie to python. I need help to solve this issue.
Upvotes: 0
Views: 1393
Reputation: 385950
There is nothing built-in to robot to handle file locking. There's also no built-in cross-platform mechanism for file locking. However, there are external packages that can do file locking. For example, the lockfile package looks like a good candidate.
What I recommend is to create a custom keyword in python that does the reading after acquiring the lock. It might look something like this if you're using the lockfile package:
# lock_keywords.py
from lockfile import LockFile
def read_file_with_locking(filename):
lock = LockFile(filename)
with lock:
with open(filename, "r") as f:
data = f.read()
return data
You would then use it in a test like this:
*** Settings ***
| Library | lock_keywords.py
*** Test Cases ***
| Read a file, with locking
| | ${data}= | Read file with locking | /tmp/junk.txt
| | log | data: ${data}
I only tested this on a Mac, by opening an interactive python shell and acquiring a lock on the file using the lockfile library, and then running the test in a different window. The test paused until I manually released the lock in the interactive session, at which point it was able to acquire the lock and read the file.
Upvotes: 1