Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created October 17, 2019 08:49
Show Gist options
  • Select an option

  • Save rondreas/4702109c38ffe04e1e3475ec72bab547 to your computer and use it in GitHub Desktop.

Select an option

Save rondreas/4702109c38ffe04e1e3475ec72bab547 to your computer and use it in GitHub Desktop.
Test to see that that official p4 api can be executed multithreadded
"""
Test to see that the official p4 api can be executed multithreadded
"""
# Get time so we can cause a delay, and uniform to set it to a random float,
from time import sleep
from random import uniform
# Get the required modules for official perforce api
from P4 import P4
from P4 import P4Exception
# Import the async multiprocessing,
import concurrent.futures
messages = (
"First!",
"Second!"
)
# Instantiate and connect to perforce,
p4 = P4()
p4.connect()
def info(sleep_for, message):
sleep(sleep_for)
return p4.run_info()
def main():
with concurrent.futures.ThreadPoolExecutor(max_workers = 2) as executor:
tasks = {executor.submit(info, uniform(1,2), msg): msg for msg in messages}
for future in concurrent.futures.as_completed(tasks):
message = tasks[future]
try:
data = future.result()
except Exception as e:
print("{} generated an exception: {}".format(message, e))
else:
print(message)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment