首页/文章/ 详情

找到原因了---Python p2pLink类的通讯问题

1年前浏览1897

1 引言

在《程序之间的相互通讯方法---FISH socket与Python》中讨论了使用p2pLink类通讯时遇到的问题。抽烟的时候在想,既然import itasca as it能够正常运行,说明itasca类已经输入到环境中,因此from itasca import p2pLinkServer出错的原因可能是p2pLinkServer这个模块名拼写有错误,查手册发现拼写正确,但意外发现问题出在itasca类上,p2pLinkServer模块没有直接继承itasca类,而是继承itasca.util库,至此问题得以解决。

2 itasca.util库

itasca.util库用于Itasca软件的连接。该库通过socket实现了Itasca软件之间的连接,这些软件包括FLAC, FLAC3D, PFC2D, PFC3D, UDEC & 3DEC。对应的类有:

FLAC3D_Connection

FLAC_Connection

FishBinaryReader

FishBinaryWriter

PFC2D_Connection

PFC3D_Connection

ThreeDEC_Connection

UDECFishBinaryReader

UDECFishBinaryWriter

UDEC_Connection

p2pLinkClient

p2pLinkServer

(1) 使用FISH将TCP socket连接到所有Itasca代码

FLAC_Connection、FLAC3D_Connection、PFC2D_Connection,PFC3D_Connection、UDEC_Connection和threeDEC_Connection这些类允许Python连接到Itasca代码并与FISH程序交换信息。数据类型在FISH和Python之间进行转换,支持int、float、str和长度为2和3的向量。

类FishBinaryReader和FishBinaryWriter允许Python读取和写入FISH二进制数据。

UDEC使用了不同的整型尺寸因此提供了特殊的类UDECFishBinaryReader和UDECFishBinaryWriter。

(2) Python to Python的socket连接

p2pLinkClient 和 p2pLinkServer 完全用Pyhton实现p2p连接。

3 通过Python p2pLink类通讯

在大多数情况下我们可以直接使用p2pLinkClient 和 p2pLinkServer类完全用Pyhton实现p2p连接。

首先在3DEC中运行服务器端代码:

from itasca.util import p2pLinkServer

import numpy as np

with p2pLinkServer() as s:

    s.start()

    while True:

        a = s.read_data()

        if type(a) is int and a ==-1:

            print("done")

            break

        print(f"got {a}")

        if type(a) is np.ndarray:

            print(a.shape)

然后在外部运行客户端代码:

from itasca import p2pLinkClient

import numpy as np

with p2pLinkClient() as s:

    s.connect("localhost")

    s.send_data("GeotechWu")

    s.send_data(np.array([1,2,3]))

    adata = np.random.random((1000,1000))

    print(adata)

    s.send_data(adata)

    for i in range(10):

        print(f"sent {i}")

        s.send_data(i)

    s.send_data(-1)

运行结果如下图所示:

本文相关文档:

程序之间的相互通讯方法---FISH socket与Python

FLAC3D 7.0 新特性简介(P4)---软件集成

FLAC3D与Python的集成 (1)---简述

FLAC3D与Python的集成 (2)---数据交换至Numpy

清理Itasca软件的冗余数据

来源:计算岩土力学
PFCFLAC3D3DEC
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2022-10-10
最近编辑:1年前
计算岩土力学
传播岩土工程教育理念、工程分析...
获赞 138粉丝 982文章 1776课程 0
点赞
收藏
未登录
1条评论
Ba tanguy
签名征集中
3月前
大佬,python中只运行import itasca as it这句没问题,但在函数里重复用it.command(" ")命令,就会出现NameError: name 'it' is not defined 这是什么问题
回复
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈