Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 701ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
m

multiprocessing

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 multiprocessing.Pipe 進行進程間通訊

Last active 5 hours ago concurrency ipc multiprocessing parallel-processing pipe python

multiprocessing.Pipe 允許 Python 進程之間傳遞資料,適用於需要高效能、雙向通訊的場景,如併發運算或分佈式處理。

0 0 0 1
1 from multiprocessing import Process, Pipe
2
3 def worker(conn):
4 conn.send("Hello from child process") # 傳送訊息
5 msg = conn.recv() # 接收訊息
6 print(f"Child received: {msg}")
7 conn.close()
8
9 if __name__ == "__main__":
10 parent_conn, child_conn = Pipe() # 建立 Pipe 雙向通道

timmy / 使用 multiprocessing 啟動多個 Flask 伺服器

Last active 5 hours ago api flask multiprocessing parallel python server

此程式使用 multiprocessing 啟動 3 個獨立的 Flask 伺服器,分別監聽 5000、5001 和 5002 埠號,讓不同的請求可以並行處理,提高效能,適用於多埠測試或模擬多個 API 服務。

0 0 0 1
1 from flask import Flask
2 import multiprocessing
3
4 # 建立 Flask 應用程式
5 def create_app(port):
6 app = Flask(__name__)
7
8 @app.route('/')
9 def home():
10 return f"Hello from port {port}!"