我有两个过程:
1)收听网络套接字中的新消息
2)将新消息发布到特定的网址
我想并行运行它们。我应该使用什么?多处理,线程还是异步?
import requests
import websocket
import time
data = []
def receive_messages(url):
ws = websocket.create_connection(url)
while True:
try:
msg = ws.recv()
except WebSocketConnectionClosedException:
ws = websocket.create_connection(url)
continue
data.append(msg)
def send_messages(url):
while True:
try:
msg = data.pop()
except IndexError:
time.sleep(1)
continue
requests.post(url, payload=msg)