1

So i am working on load balancing on cloud in which i will have multiple tasks or cloudlets and they will run on different VMs available. I have created cloudlets as follows:

cloudlets = {}

cloudlets_no = 400
a = ""
for x in range(cloudlets_no):
    a = "cloudlet_id_{}".format(x)

    cloudlets[a] = 1000 + (random.randint(0,100))


sorted_keys = []

sorted_cloudlets = sorted(cloudlets, key=cloudlets.__getitem__)
for k in sorted_cloudlets:
    sorted_keys.append(k)
    print "{} : {}".format(k, cloudlets[k])

Now to execute these task on VMs(initially a single VM) i did,

start = time.time()
for x in sorted_keys:
    rem = cloudlets[x]
    while rem != 0:
        rem -= 1

end = time.time()

Now the thing is i want to create multiple VMs and i want to run them simultaneously that's why i need to run multiple loops at once. is there a way i can achieve this or any other possible workaround.

Any help is appreciated.

TrustTyler
  • 55
  • 5
  • Your question (maybe) is already answered here: [How to use threading in Python?](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python?noredirect=1&lq=1) – thilo_dual Sep 11 '17 at 15:04
  • Possible duplicate of [How to use threading in Python?](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) – thilo_dual Sep 11 '17 at 15:05

1 Answers1

0

The 'general' topic area you want to look into is threading.

Threads allow your code to branch, and run multiple sets of instructions at the same time. It is a complex subject to go into detail on, but the basic idea is each 'thread' of processing runs separately and at the same time as the others (your operating system is in charge of how to actually achieve this on the hardware).

In python, the main modules responsible for threading are:

threading - https://docs.python.org/2/library/threading.html#module-threading

multiprocessing - https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing

There is of course far too much to mention here, but hopefully there are enough keywords in this answer to get you started researching.