Skip to content Skip to sidebar Skip to footer

Python Client Wait for Read and Write

Summary: in this tutorial, y'all'll larn how to utilize the Python threading module to develop multi-threaded applications.

Unmarried-threaded applications

Let'southward outset with a simple program:

            

from time import sleep, perf_counter def chore (): impress('Starting a task...') slumber(ane) impress('washed') start_time = perf_counter() task() task() end_time = perf_counter() print(f'It took {end_time- start_time: 0.iif} 2nd(s) to complete.')

Code language: Python ( python )

How it works.

First, import the slumber() and perf_counter() functions from the time module:

            

from time import sleep, perf_counter

Code language: Python ( python )

Second, define a part that takes one 2d to complete:

            

def task (): print('Starting a task...') sleep(i) print('done')

Lawmaking language: Python ( python )

Third, get the value of the operation counter by calling the perf_counter() function:

            

start_time = perf_counter()

Code language: Python ( python )

Fourth, telephone call the job() function twice:

            

job() task()

Lawmaking language: Python ( python )

5th, get the value of the performance counter calling the perf_counter() function:

            

end_time = perf_counter()

Code language: Python ( python )

Finally, output the time that takes to complete running the task() function twice:

            

print(f'It took {end_time- start_time: 0.2f} second(south) to complete.')

Code language: Python ( python )

Here is the output:

            

Starting a task... done Starting a task... done Information technology took ii.00 2d(s) to complete.

Code language: Python ( python )

Every bit y'all may expect, the program takes about two seconds to complete. If yous call the task() part ten times, it would take about 10 seconds to complete.

The following diagram illustrates how the program works:

First, the chore() function executed and slept for 1 second. Then information technology executed a second fourth dimension and also slept for another second. Finally, the programme completes.

When the task() function called the slumber() function, the CPU didn't do anything. This is not efficient.

This programme has one process with a unmarried thread, which is called the chief thread.

Because the program has merely 1 thread, information technology's called the single-threaded program.

Using Python threading to develop a multi-threaded program instance

To create a multi-threaded programme, you need to utilise the Python threading module.

First, import the Thread class from the threading module:

            

from threading import Thread

Code language: Python ( python )

2nd, create a new thread by instantiating an instance of the Thread grade:

            

new_thread = Thread(target=fn,args=args_tuple)

Lawmaking language: Python ( python )

The Thread() accepts many parameters. The main ones are:

  • target: specifies a office (fn) to run in the new thread.
  • args: specifies the arguments of the function (fn). The args statement is a tuple.

Tertiary, start the thread by calling the get-go() method of the Thread instance:

            

new_thread.start()

Lawmaking language: Python ( python )

If you want to expect for the thread to complete in the main thread, you can call the join() method:

            

new_thread.join()

Code language: Python ( python )

By calling the join() method, the main thread volition wait for the second thread to complete before it is terminated.

The following program illustrates how to use the threading module:

            

from time import sleep, perf_counter from threading import Thread def task (): print('Starting a task...') slumber(1) print('washed') start_time = perf_counter() # create 2 new threads t1 = Thread(target=chore) t2 = Thread(target=task) # get-go the threads t1.start() t2.start() # look for the threads to complete t1.bring together() t2.join() end_time = perf_counter() print(f'It took {end_time- start_time: 0.iif} second(s) to complete.')

Code language: Python ( python )

How information technology works. (and we'll focus on the threading part but)

First, create two new threads:

            

t1 = Thread(target=task) t2 = Thread(target=task)

2nd, offset both threads by calling the start() method:

            

t1.first() t2.start()

Lawmaking language: Python ( python )

Third, wait for both threads to complete:

            

t1.join() t2.join()

Code language: Python ( python )

Finally, show the executing time:

            

impress(f'It took {end_time- start_time: 0.twof} second(s) to complete.')

Lawmaking linguistic communication: Python ( python )

Output:

            

Starting a job... Starting a task... done done It took 1.00 second(south) to complete.

Code language: Python ( python )

When the program executes, it'll have three threads: the principal thread is created past the Python interpreter, and ii new threads are created past the program.

Equally shown clearly from the output, the plan took one 2nd instead of 2 to complete.

The following diagram shows how threads execute:

Passing arguments to threads

The following program illustrates how to pass arguments to the office assigned to a thread:

            

from time import sleep, perf_counter from threading import Thread def task (id): impress(f'Starting the task {id}...') slumber(1) impress('done') start_time = perf_counter() # create and start 10 threads threads = [] for north in range(1, 11): t = Thread(target=task, args=(north,)) threads.append(t) t.start() # look for the threads to consummate for t in threads: t.join() end_time = perf_counter() print(f'It took {end_time- start_time: 0.twof} 2d(south) to complete.')

Code language: Python ( python )

How information technology works.

Kickoff, define a task() function that accepts an argument:

            

def task (id): impress(f'Starting the task {id}...') sleep(1) print('done')

Code language: Python ( python )

Second, create 10 new threads and pass an id to each. The threads list is used to proceed track of all newly created threads:

            

threads = [] for n in range(1, eleven): t = Thread(target=task, args=(n,)) threads.suspend(t) t.start()

Code language: Python ( python )

Notice that if you phone call the join() method within the loop, the plan will wait for the first thread to complete before starting the next one.

Third, wait for all threads to complete by calling the join() method:

            

for t in threads: t.join()

Code linguistic communication: Python ( python )

The post-obit shows the output of the program:

            

Starting the task ane... Starting the task 2... Starting the task 3... Starting the job 4... Starting the task 5... Starting the task half-dozen... Starting the task 7... Starting the task 8... Starting the task 9... Starting the job ten... done done done done done washed done washed done done It took i.05 second(due south) to complete.

Lawmaking linguistic communication: Python ( python )

Information technology just took ane.05 seconds to complete.

When to utilize Python threading

As introduced in the procedure and thread tutorial, there're two main tasks:

  • I/O-bound tasks – the time spending on I/O significantly more than the time spending on computation
  • CPU-bound tasks – the time spending on computation is significantly higher than the time waiting for I/O.

Python threading is optimized for I/O bound tasks. For case, requesting remote resource, connecting a database server, or reading and writing files.

A Practical Python threading instance

Suppose that you take a list of text files in a folder e.m., C:/temp/. And yous want to supplant a text with a new ane in all the files.

The post-obit unmarried-threaded plan shows how to replace a substring with the new one in the text files:

            

from time import perf_counter def replace (filename, substr, new_substr): impress(f'Processing the file {filename}') # get the contents of the file with open(filename, 'r') as f: content = f.read() # replace the substr by new_substr content = content.supercede(substr, new_substr) # write data into the file with open(filename, 'w') as f: f.write(content) def main (): filenames = [ 'c:/temp/test1.txt', 'c:/temp/test2.txt', 'c:/temp/test3.txt', 'c:/temp/test4.txt', 'c:/temp/test5.txt', 'c:/temp/test6.txt', 'c:/temp/test7.txt', 'c:/temp/test8.txt', 'c:/temp/test9.txt', 'c:/temp/test10.txt', ] for filename in filenames: replace(filename, 'ids', 'id') if __name__ == "__main__": start_time = perf_counter() main() end_time = perf_counter() print(f'Information technology took {end_time- start_time :0.2f} 2nd(s) to consummate.')

Code linguistic communication: Python ( python )

Output:

            

It took 0.16 second(s) to complete.

Lawmaking linguistic communication: Python ( python )

The following program has the same functionality. However, it uses multiple threads instead:

            

from threading import Thread from time import perf_counter def supervene upon (filename, substr, new_substr): print(f'Processing the file {filename}') # go the contents of the file with open(filename, 'r') as f: content = f.read() # replace the substr by new_substr content = content.replace(substr, new_substr) # write data into the file with open(filename, 'westward') every bit f: f.write(content) def main (): filenames = [ 'c:/temp/test1.txt', 'c:/temp/test2.txt', 'c:/temp/test3.txt', 'c:/temp/test4.txt', 'c:/temp/test5.txt', 'c:/temp/test6.txt', 'c:/temp/test7.txt', 'c:/temp/test8.txt', 'c:/temp/test9.txt', 'c:/temp/test10.txt', ] # create threads threads = [Thread(target=supervene upon, args=(filename, 'id', 'ids')) for filename in filenames] # commencement the threads for thread in threads: thread.start() # expect for the threads to complete for thread in threads: thread.join() if __name__ == "__main__": start_time = perf_counter() primary() end_time = perf_counter() print(f'It took {end_time- start_time :0.2f} second(s) to complete.')

Code linguistic communication: Python ( python )

Output:

            

Processing the file c:/temp/test1.txt Processing the file c:/temp/test2.txt Processing the file c:/temp/test3.txt Processing the file c:/temp/test4.txt Processing the file c:/temp/test5.txt Processing the file c:/temp/test6.txt Processing the file c:/temp/test7.txt Processing the file c:/temp/test8.txt Processing the file c:/temp/test9.txt Processing the file c:/temp/test10.txt It took 0.02 second(s) to complete.

Lawmaking language: Python ( python )

As you lot tin can run across conspicuously from the output, the multi-thread programme runs so much faster.

Summary

  • Use the Python threading module to create a multi-threaded application.
  • Utilise the Thread(function, args) to create a new thread.
  • Phone call the start() method of the Thread to starting time the thread.
  • Phone call the join() method o the Thread to expect for the thread to consummate in the main thread.
  • Only use threading for I/O bound processing applications.

Did you lot find this tutorial helpful ?

sawyerlaure1962.blogspot.com

Source: https://www.pythontutorial.net/advanced-python/python-threading/

Post a Comment for "Python Client Wait for Read and Write"