Question

Concurrent Powers The following code returns the results of the integers in a list of values...

Concurrent Powers

The following code returns the results of the integers in a list of values to the power of input p. Rewrite this as concurrent code in Python, using concurrent.futures. In the concurrent version, it is not necessary for results to be printed in the same sequence as is output by the code below.

values = [2,4,6,8,10]

def powers(val,po):

    return val ** po

def non_concurrent_powers(p):

    for val in values:

        res = powers(val,p)

        print('%s to the power of %s is %s' % (val,p,res), '\n')

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello dear.

Here is the Modified Python program for implementing concurrent code in Python, using concurrent.futures. I have keep the old (non_concurrent_powers() ) mentod also. Created a new function called concurrent_powers(). Hope you will like it.

main.py

from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import as_completed

values = [2,4,6,8,10]

def powers(val,po):
    return val ** po
#non concurrent funtions
def non_concurrent_powers(p):
    for val in values:
        res = powers(val,p)
        print('%s to the power of %s is %s' % (val,p,res), '\n')

#concurrent funtions
def concurrent_powers(p):
    #for map funtion
    ps = [p]*len(values)
    with ProcessPoolExecutor(max_workers= len(values)) as executor:
        #map function is used to apply powers() function to every value in the values, ps arrays.
        results = executor.map(powers, values, ps)
    #print results
    i=0
    for result in results:
        #print(result)
        print('%s to the power of %s is %s' % (values[i],p,result), '\n')
        i+=1


if __name__ == '__main__':
    
    non_concurrent_powers(2)
    concurrent_powers(2)

Output:

2 to the power of 2 is 4 4 to the power of 2 is 16 6 to the power of 2 is 36 8 to the power of 2 is 64 10 to the power of 2 i

Image of code:

File Edit Format Run Options Window Help from concurrent. futures import Process PoolExecutor from concurrent. futures import

Hope you like it.


If you have any doubts ask me in comment section.
If you like my work, please give me a like and feedback. That helps me a lot. Thank you.
All the best.

Add a comment
Know the answer?
Add Answer to:
Concurrent Powers The following code returns the results of the integers in a list of values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following Python program, where x is assumed to be a list of integers. def...

    Consider the following Python program, where x is assumed to be a list of integers. def mystery_func(x): n = len(x) for i in range(n - 1): for j in range(i + 1, n): if x[j] - x[i] < j - i: return False return True 3a. In plain English, describe what it accomplishes (i.e., the relationship between input and output, not how the code works). 3b. Analyze the running time and express it with big O. 3c. Rewrite this function...

  • Write a program that first gets a list of integers from input. The input begins with...

    Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers in the list, namely 50, 60, 140, 200, and 75....

  • 9c Python 1) What is the output of the following code? Refer to the Weapon, Blaster,...

    9c Python 1) What is the output of the following code? Refer to the Weapon, Blaster, and Bowcaster classes. File 1: weapon.py class Weapon: def init ( self, power ): self.mPower = power return Example: import blaster from bowcaster import Bowcaster def getPower( self ): return self.mPower han = blaster.Blaster( 8000, 20 ) print("HP:", han.getPower( ) ) print( "HR:", han.getFireRate()) chewie = Bowcaster( 3000, 6) print( "CB:", chewie.getBarrelSize()) print( "CR:", chewie.getRange()) File 2: blaster.py import weapon class Blaster( weapon. Weapon):...

  • *Coral Language* Write a program that first gets a list of six integers from input. The first five values are the intege...

    *Coral Language* Write a program that first gets a list of six integers from input. The first five values are the integer list. The last value is the upper threshold. Then output all integers less than or equal to the threshold value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Such functionality is common on sites like...

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A),...

    Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be only in the form of list objects. Note: Make sure you are familiar with some of Python's basic built-in list functions and operators, as they will make completing this problem far easier. For example: Test Result A = [0, 1, 2] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], [0], [0, 1], [2],...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • 40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try:...

    40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try: print(int(answer) ) answer input ( " Enter age: ") except Value E rror : print ("Invalid answer 'q' age") Show what is printed if the user enters the following values at the prompt, one at a time. Print output User input 20 17 2a => 29 q 9. The following main function works with a class called Student that you will write. def main()...

  • #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and...

    #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and a single integer. #For this description, we'll call the single integer n. # #next_fib should modify the list to add the next n pseudo- #Fibonacci numbers to the end of the sequence. A pseudo- #Fibonacci number is the sum of the previous two numbers in #the sequence, but in our case, the previous two numbers may #not be the original numbers from the Fibonacci...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT