这些简单的节点和python包可以让您轻松地运行python for node

nodePythonCollaboration的Python项目详细描述


COLAB

这些简单的node和python包将使您可以轻松地为node运行python

建立在zeroRPC

之上

要真正运行这个包,您需要同时使用nodepython版本

功能

  • 从节点调用python函数
  • 修饰python函数以获得更好的结果

安装

使用NPM:

$ npm install node-python-collaboration

使用pip:

$ pip install nodePythonCollaboration

快速启动

节点客户端(带同步设置)

constcolab=require('node-python-collaboration')// Configure methods object synchronouslyconstmethods=colab([['test_method',false],],/*options*/)// Call some methodsmethods.test_method(1).then(res=>console.log(res))// logs 2.catch(err=>console.log(err))

节点客户端(带异步设置)

Note that in async setup client get methods from python server

constcolab=require('node-python-collaboration')asyncfunctionmain(){try{// Configure methods object asynchronouslyconstmethods=awaitcolab.asyncInit(/*options*/)// Call some methodsconsole.log(awaitmethods.test_method(1))// logs 2}catch(err){console.log(err)}}main()

python服务器端

importnodePythonCollaborationascolab# Define main class with methodsclassmain():deftest_method(self,x):returnx+1# Start Python server with those methodscolab.start(main)

COLAB函数

在node.js中:


colab(方法r,{选项})

返回一个具有属性的对象,这些属性是在方法名中给定名称的函数

methodsArr

[[method_name,method_type],[method_name,method_type]...]

options可选需要与python匹配

// those are default options{port:'4242',server:'127.0.0.1'// it is recommended not to change}

Example

methods=colab([['test_method',false],['test_method_2',false]],{port:'4242'})// Do some stuff with methods

methods=colab([['test_method',false],['test_method_2',false]])// Do some stuff with methods

colab.asyncinit({options})

当promise被解析时返回promise promise返回方法

在async init中,我们不需要给出methodsarr,因为colab要求提供可用的方法我们的Python server

Example

colab.asyncInit(/*options*/).then((methods)=>{// Do some stuff with methods})

异步/等待的更好方法

asyncfunctionmain(){methods=awaitcolab.asyncInit(/*options*/)// Do some stuff with methods}main()

方法

  • 类型

    • 错误

      最快的

      [[method_name,false]]

      这只是普通的方法没有什么特别的

    • 完整文件

      [[method_name,'complexFile']]

      如果出现错误“overflowerror:python int太大,无法转换为c unsigned long”
      这就是解决方案记住,在python端,您需要使用方法decorator

      基本上是这样做的:python将输出保存到/data文件夹中的文件,然后将文件名发送到节点 这样node可以读取文件,然后将其删除

    • 大计算

      最慢的

      [[method_name,'bigComputation']]

      如果出现错误“HeartBeatERROR:10000毫秒后失去遥控器”
      这就是解决方案记住,在python端,您需要使用方法decorator

      基本上它的作用是:python在另一个进程中运行您的函数,这样它就不会阻塞主进程 然后将输出保存到文件中

  • 概述

    • 返回承诺
    • 执行python函数in python
    • 给方法的参数被传递给python
  • 示例

    constcolab=require('node-python-collaboration')colab([['test_method',false]]).then(methods=>{methods.test_method(1).then(response=>console.log(response))//logs 2})

    异步/等待的更好方法

    constcolab=require('node-python-collaboration')asyncfunctionmain(){methods=awaitcolab.asyncInit()console.log(methods.test_method(1))//logs 2}main()

在python中:


Colab.开始(类,选项arr)

使用127.0.0.1:4242中的默认类方法启动python服务器

class

classmain():deftest_method(self,x):returnx+1

现在方法“test_method”可用

optionsArr可选

optionsArr=[data_folder_path,server_ip_and_port]#defining new data flder pathoptionsArr=['./test/test/data']#defining new server port (data folder path stay default)optionsArr=[None,':5323']#defining new server ip and port (data folder path stay default)optionsArr=[None,'0.0.0.0:5323']# it's very not recommeded to change ip, in most cases it won't run

Colab装饰工

它们在上面的节点部分中进行了描述

  • @colab.complexfile
  • @colab.bigcompulation

Example

importnodePythonCollaborationascolabfrommathimportfactorialfromtimeimportsleepclassmain():defExample(self,x,y):returnx+y# Just simple computation@colab.complexFiledefExample_complexFile(self,x):returnfactorial(x)# In most cases that will be very big number@colab.bigComputationdefExample_bigComputation(self,x,y):foriinrange(11):print(i+1)sleep(1)# Simulating big computationreturnx+ycolab.start(main)

节点和python:

Example

服务器代码

importnodePythonCollaborationascolab# import colabfrommathimportfactorialfromtimeimportsleepclassmain():# define methodsdefExample(self,x,y):returnx+y@colab.complexFiledefExample_complexFile(self,x):returnfactorial(x)@colab.bigComputationdefExample_bigComputation(self,x,y):foriinrange(11):print(i+1)sleep(1)returnx+ycolab.start(main)# start server with default options

客户端代码

constcolab=require('node-python-collaboration')// import colabasyncfunctionmain(){try{// Configure methods object asynchronously with default optionsconstmethods=awaitcolab.asyncInit()console.log(awaitmethods.Example(2,2))//logs 4console.log(awaitmethods.Example_complexFile(10000))//logs 2846259680917054518906413212119868890148051401702799230794179994274411340003764443772990786757784775815884062142317528830042339940...console.log(awaitmethods.Example_bigComputation(2,2))//after 11s logs 4}catch(err){console.log(err)}}main()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java迭代图形框架聚合达到内存限制的消息   反编译和开发人员向Android应用程序提供java安全密钥   活页夹中的java Vaadin上传组件,例如Crud编辑器   java如何定义0090当我有了每个字符   带有AOP的java截取私有注释方法   在Java中暂停并恢复动画(thread.sleep)   java在JavaFX中从不同的控制器向TableView添加行   java如何通过Spring使用脚本初始化inmemory HSQLDB   windows找到了绝对的java。通过编程从java代码获取exe路径   Java同步、信号量和队列的多线程性能   java异步任务回调未调用   java在迷宫中用坐标寻找最短路径   Java:并行处理数组,查找发生异常的位置   java我无法理解我收到的错误   如何调用。bat文件,并使用java中的ProcessBuilder发送字符串   java在mysql数据库中插入日期   将ArrayList的内容显示为格式正确的JSON for Java REST API   java@OneToMany注释SQLSyntaxErrorException:ORA000904无效标识符