pymemcache不支持缓存python对象

2024-05-18 12:35:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用this教程在服务器上安装和配置memcached服务。然后我尝试通过pymemcached包与memcached交互

我将此代码用作process1.py

from time import sleep
from pymemcache.client.base import Client
from pymemcache import serde
import cv2
# import matplotlib.pyplot as plt


client = Client(('127.0.0.1', 11211),
                serializer=serde.python_memcache_serializer,
                deserializer=serde.python_memcache_deserializer)

# result = client.get('some_key')
cap = cv2.VideoCapture('video/vdo.mp4')

while True:
    ret, frame = cap.read()
    client.set("image", frame)
    print("cached")

    if not ret:
        break

    # cv2.imshow('Frame', frame)

    # Press Q on keyboard to  exit
    # if cv2.waitKey(25) & 0xFF == ord('q'):
    #     break

    sleep(0.05)

cap.release()

# Closes all the frames
cv2.destroyAllWindows()

这一个是process2.py

import subprocess
import numpy as np
import cv2
from flask import Flask, Response
# from process1 import client

from pymemcache.client.base import Client
from pymemcache import serde

app = Flask(__name__)


client = Client(('127.0.0.1', 11211),
                serializer=serde.python_memcache_serializer,
                deserializer=serde.python_memcache_deserializer)


def gen():
    while True:
        # Capture frame-by-frame
        img = np.asarray(client.get('image'), np.uint8)
        if img is not None:
            # img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
            frame = cv2.imencode('.jpg', img)[1].tobytes()
            yield b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'


@app.route('/')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


py_dir = "/home/masoud/anaconda3/envs/py37/bin/python"
process_dir = "/home/masoud/Desktop/PycharmProjects/share_memory_test"

if __name__ == '__main__':
    cmd = [py_dir, process_dir]
    subprocess.Popen(cmd)
    app.run(host='127.0.0.1', port=8888, debug=True)

奇怪的是,我在自己的笔记本电脑Ubuntu18上运行了这段代码,没有问题。但当我在办公室的一台服务器上运行(使用ubuntu 16.04)时,我遇到了以下运行时错误:

    File "codes/MSD/process1.py", line 139, in run
    client.set("stream_image_cached", a)
  File "anaconda3/envs/msd_ped_det/lib/python3.7/site-packages/pymemcache/client/base.py", 
line 341, in set flags=flags)[key]
  File "anaconda3/envs/msd_ped_det/lib/python3.7/site-packages/pymemcache/client/base.py", 
line 933, in _store_cmd self.sock.sendall(b''.join(cmds)) 

ConnectionResetError: [Errno 104] Connection reset by peer

我不知道还有什么地方需要检查才能让它工作


Tags: frompyimageimportclientimgbasecv2

热门问题