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
  • 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...

  • 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()...

  • 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...

  • + Run C Code IMPORTANT: • Run the following code cell to create the input file,...

    + Run C Code IMPORTANT: • Run the following code cell to create the input file, biostats.csv, which you will be using later. 74, In [ ]: N %%file biostats.csv Name, Sex, Age, Alex, M, 41, Bert, M, 42, Dave, M, 39, Elly, F, 30, Fran, F, 33, Jake, M, F, Luke, M, 34, F Myra, M, M, 38, Ruth, F, 28, 22 22 323 47 47, Height, Weight 170 200 167 70 115 143 139 280 98 75, 350...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • 8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za...

    8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...

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
ADVERTISEMENT