An example in Python

While we will go into more depth regarding how asynchronous programming can be implemented in Python and the main tools we will be using, including the asyncio module, let's consider how asynchronous programming can improve the execution time of our Python programs.

Let's take a look at the Chapter09/example1.py file:

# Chapter09/example1.py

from math import sqrt

def is_prime(x):
print('Processing %i...' % x)

if x < 2:
print('%i is not a prime number.' % x)

elif x == 2:
print('%i is a prime number.' % x)

elif x % 2 == 0:
print('%i is not a prime number.' % x)

else:
limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
print('%i is not a prime number.' % x)
return

print('%i is a prime number.' % x)

if __name__ == '__main__':

is_prime(9637529763296797)
is_prime(427920331)
is_prime(157)

Here, we have our familiar prime-checking is_prime() function, which takes in an integer and prints out a message indicating whether that input is a prime number or not. In our main program, we call is_prime() on three different numbers. We are also keeping track of how much time it takes for our program to process all three numbers.

Once you execute the script, your output should be similar to the following:

> python example1.py
Processing 9637529763296797...
9637529763296797 is a prime number.
Processing 427920331...
427920331 is a prime number.
Processing 157...
157 is a prime number.

You have probably noticed that the program took quite some time to process the first input. Because of the way the is_prime() function is implemented, if the input of the prime number is large, then it takes is_prime() longer to process it. So, since we have a large prime number as the first input, our Python program will hang for a significant amount of time before printing out the output. This typically creates a non-responsive feel for our program, which is not desirable in both software engineering and web development.

To improve the responsiveness of the program, we will take advantage of the asyncio module, which has been implemented in the Chapter09/example2.py file:

# Chapter09/example2.py

from math import sqrt

import asyncio

async def is_prime(x):
print('Processing %i...' % x)

if x < 2:
print('%i is not a prime number.' % x)

elif x == 2:
print('%i is a prime number.' % x)

elif x % 2 == 0:
print('%i is not a prime number.' % x)

else:
limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
print('%i is not a prime number.' % x)
return
elif i % 100000 == 1:
#print('Here!')
await asyncio.sleep(0)

print('%i is a prime number.' % x)

async def main():

task1 = loop.create_task(is_prime(9637529763296797))
task2 = loop.create_task(is_prime(427920331))
task3 = loop.create_task(is_prime(157))

await asyncio.wait([task1, task2, task3])

if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except Exception as e:
print('There was a problem:')
print(str(e))
finally:
loop.close()

We will go into the details of this code in the next chapter. For now, simply run the script, and you will see an improvement in responsiveness in the printed output:

> python example2.py
Processing 9637529763296797...
Processing 427920331...
427920331 is a prime number.
Processing 157...
157 is a prime number.
9637529763296797 is a prime number.

Specifically, while 9637529763296797 (our largest input) was being processed, the program decided to switch to the next inputs. Therefore, the results for 427920331 and 157 were returned before it, hence improving the responsiveness of the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169