#主要代码如下
import os
from multiprocessing.dummy import Process
import threading
def talk(conn):
while 1: # 循环通讯
try:
from_client_msg = conn.recv(1024)
if not from_client_msg:
break
print("进程<%s>来自客户端的消息:%s" % (os.getpid(), from_client_msg))
conn.send(from_client_msg.upper())
except:
break
conn.close()
def socket():
import socket
# from trashCanInfo.models.trashCanInfo import TrashCanInfo
server_sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1', 9000))
server_sk.listen(5)
while 1:
new_sk, addr = server_sk.accept()
p = Process(target=talk, args=(new_sk,))
p.start()
server_sk.close()
# 创建一个线程
mthread = threading.Thread(target=socket)
# 启动刚刚创建的线程
mthread .start()
主要采用threading创建一个线程,参数target指向你的函数名;然后再利用process实现循环通信;项目需要从另一个服务器通信;socket传来一些信息建立连接,直接存入数据库。完整代码如下:在serializers.py中:
import os
from multiprocessing.dummy import Process
from rest_framework import serializers
from trashCanInfo.models import TrashCanInfo
from vadmin.op_drf.serializers import CustomModelSerializer
import threading
# ================================================= #
# ************** 项目管理 序列化器 ************** #
# ================================================= #
def talk(conn):
while 1: # 循环通讯
try:
from_client_msg = conn.recv(1024)
if not from_client_msg:
break
print("进程<%s>来自客户端的消息:%s" % (os.getpid(), from_client_msg))
conn.send(from_client_msg.upper())
except:
break
conn.close()
def socket():
import socket
# from trashCanInfo.models.trashCanInfo import TrashCanInfo
server_sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1', 9000))
server_sk.listen(5)
while 1:
new_sk, addr = server_sk.accept()
p = Process(target=talk, args=(new_sk,))
p.start()
server_sk.close()
class ProjectSerializer(CustomModelSerializer):
"""
项目管理 简单序列化器
"""
class Meta:
model = TrashCanInfo
fields = '__all__'
class ProjectCreateUpdateSerializer(CustomModelSerializer):
"""
项目管理 创建/更新时的列化器
"""
# from trashCanInfo.models.trashCanInfo import TrashCanInfo
# TrashCanInfo(id='6', chuyu="33", code="socket", dept_belong_id="1", kehuishou="666", modifier="admin",
# name="测试222", position="22", qita="22", start="1", status="N", youhai="22").save()
# TrashCanInfo.objects.create(id='6', chuyu="33", code="66666", dept_belong_id="1", kehuishou="22", modifier="admin",
# name="测试222", position="22", qita="22", start="1", status="N", youhai="22")
# Test.objects.create(code='2', code2='11')
# 此处可写定制的 创建/更新 内容
# 创建一个线程
mthread = threading.Thread(target=socket)
# 启动刚刚创建的线程
mthread .start()
def validate(self, attrs: dict):
print('serial', attrs)
return super().validate(attrs)
class Meta:
model = TrashCanInfo
fields = '__all__'
class ExportProjectSerializer(CustomModelSerializer):
"""
导出 项目管理 简单序列化器
"""
person__username = serializers.SerializerMethodField(read_only=False)
# dept__deptName = serializers.SerializerMethodField(read_only=False)
def get_person__username(self, obj):
return "" if not hasattr(obj, 'person') else obj.person.username
def get_dept__deptName(self, obj):
return "" if not hasattr(obj, 'dept') else obj.dept.deptName
class Meta:
model = TrashCanInfo
fields = ('id', 'name', 'code', 'creator', 'person__username',
'kehuishou', 'chuyu', 'youhai', 'qita', 'position', 'status', 'start')
?
|