Spawn means the creation of a process by a parent process. This video will show us how to spawn a process.
- [Instructor] Hi, welcome to the third section of this course, Process-Based Parallelism. In the previous section, we saw how to use threads to implement concurrent applications. This section will examine the process-based approach. In particular, the focus is on two libraries: the Python multiprocessing module and the Python mpi4py module. Let's begin with the first video of this section, Spawning a Process. In this video, we'll create a series, up to five processes and then run it. The term 'spawn' means the creation of a process by a parent process.
The parent process can, of course, continue its execution asynchronously or wait until the child process end its execution. The multiprocessing library of Python allows the spawning of a process through these steps: One, build the object process. Two, call its start method. This method starts the process's activity. Three, call its join method. It awaits until the process has completed it work and exited. This code example shows you how to create a series or five of processes.
Each process is associated with the function, 'function' or (i). Where i is the ID associated with the process that contains it. To run the process and display the results, let's open the command prompt, preferably in the folder containing the example file, named spawn_a_process.py and then type the command as shown. We obtain the following output using this command. Let's see how this works. As explained in the introduction section of this video, to create the object process, we must first import the multiprocessing module with this command.
Then we create the object process in the main program using this command. Further, we call the start method as shown. The object process has, for argument, the function to which the child process is associated. We also pass an argument to the function that it takes into account the process in which the associated function is situated. Finally, we call the join method on a process created. Without p.joint method, the child process will sit idle and not be terminated and then you must manually kill it. This reminds us once again of the importance of instantiating the process object within the main section.
This is because the child process created imports the script file where the target function is contained. Then, by instantiating the process object within this block, we prevent an infinite recursive call of such instantiations. A valid workaround is used to define the target function in a different script. And then it imports it into the main space. So for our example, we could have this. Here, target_function.py is as shown. Let's run the code now.
You can see the output is always similar to that shown here. Great! So in this video, we saw the process of spawning. In our next video, we'll check out how to name a process.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Memory organization
- Parallel programming models
- Designing a parallel program and evaluating performance
- Working with threads in Python
- Synchronizing threads and using multithreading
- Spawning a process
- Running a process in the background
- Synchronizing processes
- Using the mpi4py Python module
- Using collective communication
- Reducing operations
- Managing events, tasks, and routines with Asyncio
- Distributing tasks
Skill Level Intermediate
1. Getting Started with Parallel Computing and Python
2. Thread-Based Parallelism
3. Process-Based Parallelism
4. Asynchronous Programming
5. Distributed Python
6. GPU Programming with Python
Using the PyCUDA module7m 33s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.