python 两个脚本同时执行,并且 互相通信

作者&投稿:龙勉 (若有异议请与网页底部的电邮联系)
怎样实现两个PY文件之间通信,不要一个文件中的多线程~

使用多线程,下面的代码,简单实现一个多线程的web服务器:


#coding=utf-8import socketimport threadingfrom time import sleepdef response(sock, addr): print "收到请求" data = sock.recv(1024) print data sock.send(html) sock.close()html = '''HTTP/1.1 200 OK
Content-Type: text/html

Hello world!'''s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind(('0.0.0.0', 80))s.listen(50)print "正在等待连接……"while 1: sleep(0.1) sock,addr = s.accept() t = threading.Thread(target=response, args=(sock,addr)) t.start()

如果是桌面的话,开两个窗口即可。在两个窗口分别操作运行就可以啦。
如果是linux的话,可以使用&符号(在命令行最后加上“ &”)让程序在后台运行即可。

import sys, thread()ing, queue

if sys.version_info.major == 3:
    def execfile(filename, globals=None, locals=None):
        g = globals if globals is not None else __builtins__.globals()
        l = locals if locals is not None else __builtins__.locals()
        with open(filename) as f:
            code = compile(f.read(), filename, 'exec')
            exec(code, g, l)
            
            
if __name__ == '__main__':
    f1 = '/path/to/file1.py' # MainThread
    f2 = '/path/to/file2.py'
    
    # 将需要共享的变量放入全局变量字典,在f1和f2中可以直接使用q1和q2两个队列
    # f1向f2发送消息:
    #     f1: q1.put(something) 
    #     f2: q1.get()
    # f2向f1发送消息:
    #     f2: q2.put(something) 
    #     f1: q2.get()
    g = {'q1': queue.Queue(), 'q2': queue.Queue()}
    g.update(globals())
    
    thread = threading.Thread(target=execfile, args=(f2, ), kwargs={'globals':g})
    thread.start()
    execfile(f1, globals=g)
    thread.join()



宿城区19878951150: python 两个脚本同时执行,并且 互相通信 -
许怨氯膦: import sys, threading, queue if sys.version_info.major == 3: def execfile(filename, globals=None, locals=None): g = globals if globals is not None else __builtins__.globals() l = locals if locals is not None else __builtins__.locals() with open(filename) ...

宿城区19878951150: 如何同时运行两个python程序 -
许怨氯膦: linux上么,一是你可以在命令后面加“&”让其在后台运行,还有一个方法就是用screen、让它在screen中运行

宿城区19878951150: 如何同时启动2个python脚本 -
许怨氯膦: 可以在一系统启动的几个文件里加载这个脚本.或者写成一个service加到系统服务里.如果是定时,你可以参看cron

宿城区19878951150: python 两个线程怎么同时执行 -
许怨氯膦: 科技在发展,时代在进步,我们的CPU也越来越快,CPU抱怨,P大点事儿占了我一定的时间,其实我同时干多个活都没问题的;于是,操作系统就进入了多任务时代.我们听着音乐吃着火锅的不在是梦想.python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了.

宿城区19878951150: 一台电脑如何同时使用python2idle和python3idle吗 -
许怨氯膦: 一台电脑可以同时使用python2idle和python3idle,就是在系统中装两个版本的python,这样在文件右键菜单中就会出现两个IDLE,最新版python3.5.1已经可以显示版本号, python2.x的脚本就可以用python2idle编辑,python3.x的脚本就可以用python3idle编辑.以下为安装两个版本时右键菜单的显示,上方为python2.7.11的IDLE,下方为3.5.1版本的:

宿城区19878951150: 如何在python脚本中执行另一个python脚本 -
许怨氯膦: 把两个脚本filea.py 和 fileb.py 放在同一个目录下,然后在filea.py的开头写:import fileb 然后就可以使用fileb.py中定义的函数了

宿城区19878951150: python 如何同时创建多个CMD窗口并同步执行cmd命令? -
许怨氯膦: 可以试一下这个:import os cmd_commands = [] for c in cmd_commands: os.popen(i)

宿城区19878951150: mac python shell 如何同时执行两个.py 文件 -
许怨氯膦: 你就打开两个terminal,每个terminal跑一个python文件不就好...

宿城区19878951150: Eclipse 如何运行多个python脚本 -
许怨氯膦: 无法,一个项目同时只能有一个生效解释器.你可以将它们拆分为多个项目,多次修改解释器并run.或者抛弃eclipse,在系统中编写一个脚本,完成按顺序修改python版本并运行ABC脚本的过程,只要运行这个脚本即可.

宿城区19878951150: linux cron 如果设置了同一时间的多个python脚本任务,它们会并发执行还是依次执行? -
许怨氯膦: 一般的linux的distro都是用的vixie cron,vixie cron执行命令的时候是启动一个新的进程然后就回到cron的主进程继续干活,不等子进程结束.所以你这三个命令应该是依次启动,同时执行,各自结束.具体参考vixie cron源代码的do_command.c里80行左右的void do_command(e, u)函数.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网