import os
def my_func(x, y):
print(f"Child running my_func with {x=} and {y=}")
return x * y
_test = 0
pid = os.fork()
if pid == 0:
my_func(10, 20)
print(f"In child process: {id(my_func)}")
os._exit(0)
else:
print(f"Parent: child PID is {pid}")
print(f"In parent process: {id(my_func)}")
```
```python
import time
def cpu_task():
s = time.time()
for _ in range(50_000_000):
pass
print("Process finished in", time.time() - s)
```
```python
import asyncio
async def say_after(delay, msg):
await asyncio.sleep(delay)
print(msg)
async def main():
await asyncio.gather(
say_after(2, "Hello"),
say_after(1, "World")
)
asyncio.run(main(), debug=True)
```
```python
import os
import time
pr, pw = os.pipe()
cr, cw = os.pipe()
pid = os.fork()
if pid == 0:
os.close(pw)
os.close(cr)
msg = os.read(pr, 1024)
print("Child got:", msg.decode())
reply = b"Message received!"
os.write(cw, reply)
os.close(pr)
os.close(cw)
else:
os.close(pr)
os.close(cw)
print("[Parent] Started, waiting 2 seconds before sending message...")
time.sleep(2)
os.write(pw, b"Hello child!")
reply = os.read(cr, 1024)
print("Parent got:", reply.decode())
os.close(pw)
os.close(cr)
```
```python
import os
pid = os.fork()
if pid == 0:
print("Child process")
else:
print("Parent process, child PID:", pid)
```
```python
import os
r, w = os.pipe()
pid = os.fork()
if pid == 0:
os.close(r)
message = b"Hello from child!"
os.write(w, message)
os.close(w)
else:
os.close(w)
data = os.read(r, 1024)
print("Parent received:", data.decode())
os.close(r)