在两个Python脚本之间共享数据

时间:2015-04-17 09:40:51

标签: python pickle

我目前正在运行一个日志记录脚本,将数据记录到dict中,如下所示(格式:日期,时间,数据):

{0: ['17/04/2015', '10:16:39', '', '92088380'], 1: ['17/04/2015', '10:16:52', '', '920883E1'], 2: ['17/04/2015', '10:17:04', '', '92088380'], 3: ['17/04/2015', '10:17:17', '', '920883E1'], 4: ['17/04/2015', '10:17:19', '', '6F030980'], 5: ['17/04/2015', '10:17:29', '', '92088380'], 6: ['17/04/2015', '10:17:42', '', '920883E1'], 7: ['17/04/2015', '10:17:54', '', '92088380'], 8: ['17/04/2015', '10:18:07', '', '920883E1'], 9: ['17/04/2015', '10:18:19', '', '92088380'], 10: ['17/04/2015', '10:18:32', '', '920883E1'], 11: ['17/04/2015', '10:18:44', '', '92088380'], 12: ['17/04/2015', '10:18:57', '', '920883E1'], 13: ['17/04/2015', '10:19:09', '', '92088380'], 14: ['17/04/2015', '10:19:22', '', '920883E1'], 15: ['17/04/2015', '10:15:37', '', '920883E1'], 16: ['17/04/2015', '10:15:49', '', '92088380'], 17: ['17/04/2015', '10:16:02', '', '920883E1'], 18: ['17/04/2015', '10:16:14', '', '92088380'], 19: ['17/04/2015', '10:16:27', '', '920883E1']}

我想将此变量与另一个python脚本一起使用,该脚本将独立运行到记录数据的脚本。

我尝试过不同的方法,例如将其写入文件并以简单的方式阅读,并尝试使用pickle。

然而,我想知道是否有一种方法可以在不参与编写文件的情况下共享变量,或者是否最好的方法是使用pickle?

2 个答案:

答案 0 :(得分:1)

如果你想在两个独立运行的python脚本之间传递对象,有许多python代码可以做到这一点。选择正确的代码实际上取决于您的两个脚本所在的位置,以及您希望如何传递信息。

如果您考虑在两个独立启动的脚本之间共享对象,并希望通过传递对象来共享信息,则可以使用标准python库中的subprocess模块来完成此操作。使用Popen中的subprocess,您可以在正在运行的进程之间创建管道。这是python中几种不同管道技术的基础。如果这就是你想要的,那就不要再看了。

这里列出了几个不同的分布式并行管道代码:https://wiki.python.org/moin/ParallelProcessing,每个代码的工作方式略有不同或考虑到不同的用例。现在,它取决于您希望其他脚本存在的位置,以及您希望如何与其进行通信。 这是一个很长的清单,而不是完全包容......所以为了给出一个更具体的答案,你需要做更多细节。

例如,构建pyropp(并行Python)以处理在不同计算机上运行的进程。还有其他类似的东西。以上两者主要使用subprocess和跨套接字的连接,并且可以构建“远程作业服务器”。看起来这两个软件包现在也可以跨ssh个连接运行。

答案 1 :(得分:0)

查看Pyro

Pyro means PYthon Remote Objects. It is a library that enables you to
build applications in which objects can talk to eachother over
the network, with minimal programming effort. You can just use normal
Python method calls, with almost every possible parameter and return
value type, and Pyro takes care of locating the right object on the
right computer to execute the method. It is designed to be very easy
to use, and to generally stay out of your way. But it also provides a
set of powerful features that enables you to build distributed
applications rapidly and effortlessly. Pyro is written in 100% pure
Python and therefore runs on many platforms and Python versions,
including Python 2.x, Python 3.x, IronPython, and Pypy.