Python Processes, Multiprocessing, Decorators, and List Fundamentals
This article introduces Python process concepts, including process creation with the multiprocessing module, process states, comparison with threads, and practical examples of creating, managing, and communicating with processes, followed by a comprehensive overview of Python list types, their structure, operations, and common usage patterns.
Process and State
A program is a static file (e.g., xxx.py ), while a process is the program plus the resources allocated by the operating system when it runs. Processes have three main states: ready (conditions satisfied, waiting for CPU), running (CPU executing), and waiting (e.g., sleeping).
Creating Processes with multiprocessing
The multiprocessing module provides a cross‑platform way to spawn processes. The Process class represents an independent process that can execute a target function.
<code>import multiprocessing
import time
def test():
while True:
print("--test--")
time.sleep(1)
if __name__ == "__main__":
p = multiprocessing.Process(target=test)
p.start()
while True:
print("--main--")
time.sleep(1)
</code>Key points:
Only the target function and its arguments need to be supplied.
Global variables are not shared between parent and child processes.
Process PID
<code>import multiprocessing
import os
def test():
print("Child process PID=%d" % os.getpid())
if __name__ == "__main__":
print("Parent process PID=%d" % os.getpid())
p = multiprocessing.Process(target=test)
p.start()
</code>The os.getpid() function returns the current process ID.
Process Constructor Syntax
Signature: Process([group, target, name, args, kwargs])
target : function to run in the child process.
args : tuple of positional arguments for target .
kwargs : dict of keyword arguments for target .
name : optional name for the process.
group : rarely used process group identifier.
Common Methods and Attributes
start() : launch the process.
is_alive() : check if the process is still running.
join([timeout]) : wait for the process to finish.
terminate() : forcefully stop the process.
pid : process identifier.
name : process name (default Process‑N ).
Passing Arguments to a Child Process
<code>import multiprocessing
import os
import time
def test(name, **kwargs):
for i in range(10):
print("Child process name=%s, pid=%d" % (name, os.getpid()))
print(kwargs)
time.sleep(0.2)
if __name__ == "__main__":
p = multiprocessing.Process(target=test, args=("zhangsan",), kwargs={"xxoo": 666})
p.start()
time.sleep(1)
p.terminate()
p.join()
</code>This demonstrates how to supply positional and keyword arguments.
Global Variables Are Not Shared
<code>import multiprocessing
import os
import time
g_nums = [11, 33]
def test1():
print("in test1, pid=%d, g_nums=%s" % (os.getpid(), g_nums))
for i in range(4):
g_nums.append(i)
time.sleep(1)
print("in test1, pid=%d, g_nums=%s" % (os.getpid(), g_nums))
def test2():
print("in test2, pid=%d, g_nums=%s" % (os.getpid(), g_nums))
if __name__ == "__main__":
p1 = multiprocessing.Process(target=test1)
p1.start()
p1.join()
p2 = multiprocessing.Process(target=test2)
p2.start()
</code>Each process gets its own copy of g_nums , illustrating lack of shared state.
Process vs. Thread Comparison
Functionality : Both enable multitasking; processes run separate programs, threads run concurrent tasks within a single program.
Definition : A process is an independent resource‑allocation unit; a thread is a lightweight CPU‑scheduling unit that shares the process’s memory.
Differences : Every program has at least one process and each process has at least one thread; threads have lower overhead and share memory, while processes have isolated memory spaces.
Pros/Cons : Threads are fast but harder to manage safely; processes provide better isolation and stability at the cost of higher overhead.
Decorators (Dynamic Proxies)
A decorator is a higher‑order function that wraps another function, adding behavior before or after the original call without modifying its source code.
<code>def wrapper(fn):
def inner(*args, **kwargs):
# pre‑call actions
ret = fn(*args, **kwargs)
# post‑call actions
return ret
return inner
@wrapper
def func():
print('func executed')
func()
</code>Examples include simple wrappers, wrappers with arguments, multiple decorators on one function, and decorators that preserve return values.
Decorator with Arguments
<code>def sight(fn):
def inner(*args, **kwargs):
print('Start game')
ret = fn(*args, **kwargs)
print('Exit')
return ret
return inner
@sight
def game(user, pwd):
print('Login:', user, pwd)
print('Shoot')
ret = game('bob', '123')
print(ret)
</code>Multiple Decorators on One Function
<code>def wrapper1(fn):
def inner(*args, **kwargs):
print('wrapper1-1')
ret = fn(*args, **kwargs)
print('wrapper1-2')
return ret
return inner
def wrapper2(fn):
def inner(*args, **kwargs):
print('wrapper2-1')
ret = fn(*args, **kwargs)
print('wrapper2-2')
return ret
return inner
def wrapper3(fn):
def inner(*args, **kwargs):
print('wrapper3-1')
ret = fn(*args, **kwargs)
print('wrapper3-2')
return ret
return inner
@wrapper1
@wrapper2
@wrapper3
def func():
print('core function')
func()
</code>Python List Fundamentals
A list is a mutable container that can hold any number of items of any type. Elements are stored by reference, so modifying an element does not change the list object itself.
Example of IDs before and after modification:
<code>L = ["a", "b", "c"]
print(id(L), id(L[0]) # list ID and first element ID
L[0] = "aa"
print(id(L), id(L[0]) # list ID unchanged, element ID changed
</code>Creating Lists
Using square brackets: []
Using the list() constructor.
Both allow multi‑line element definitions.
Basic Operations
Concatenation with + and repetition with * .
In‑place addition with += (more efficient for mutable sequences).
Indexing, slicing, and assignment (including slice assignment to delete elements).
Standard sequence methods: append() , extend() , remove() , pop() , reverse() , copy() , etc.
Sorting
Lists have a sort() method (in‑place) and the built‑in sorted() function (returns a new list). Both accept key and reverse arguments.
<code>L = ['python', 'shell', 'Perl', 'Go', 'PHP']
print(sorted(L)) # returns new sorted list
L.sort() # sorts in place
print(L)
# Sort by length descending
L.sort(key=len, reverse=True)
print(L)
</code>Iteration and Comprehensions
Lists support membership testing ( in ) and can be iterated with for . List comprehensions provide a concise way to build new lists.
<code># Simple comprehension
[i for i in "abcdef"] # ['a', 'b', 'c', 'd', 'e', 'f']
# With expression
[i*2 for i in [1,2,3,4]] # [2, 4, 6, 8]
</code>Comprehensions are generally faster than equivalent for loops.
--- End of summary ---
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.