Run multiple servers in python at same time (Threading)
<p>I have <strong>2 servers</strong> in python, <strong>I want to mix them up in one single .py and run together</strong>: </p>

<p>Server.py: </p>

<pre><code>import logging, time, os, sys
from yowsup.layers import YowLayerEvent, YowParallelLayer
from yowsup.layers.auth import AuthError
from yowsup.layers.network import YowNetworkLayer
from yowsup.stacks.yowstack import YowStackBuilder

from layers.notifications.notification_layer import NotificationsLayer
from router import RouteLayer

class YowsupEchoStack(object):
    def __init__(self, credentials):
        "Creates the stacks of the Yowsup Server,"
        self.credentials = credentials
        stack_builder = YowStackBuilder().pushDefaultLayers(True)

        stack_builder.push(YowParallelLayer([RouteLayer, NotificationsLayer]))
        self.stack = stack_builder.build()
        self.stack.setCredentials(credentials)

    def start(self):
        self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        try:
            logging.info("#" * 50)
            logging.info("\tServer started. Phone number: %s" % self.credentials[0])
            logging.info("#" * 50)
            self.stack.loop(timeout=0.5, discrete=0.5)
        except AuthError as e:
            logging.exception("Authentication Error: %s" % e.message)
            if "&lt;xml-not-well-formed&gt;" in str(e):
                os.execl(sys.executable, sys.executable, *sys.argv)
        except Exception as e:
            logging.exception("Unexpected Exception: %s" % e.message)

if __name__ == "__main__":
    import sys
    import config

    logging.basicConfig(stream=sys.stdout, level=config.logging_level, format=config.log_format)
    server = YowsupEchoStack(config.auth)
    while True:
        # In case of disconnect, keeps connecting...
        server.start()
        logging.info("Restarting..")
</code></pre>

<p>App.py:</p>

<pre><code>import web

urls = (
  '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        greeting = "Hello World"
        return greeting

if __name__ == "__main__":
    app.run()
</code></pre>

<p>I want to <strong>run both together from single .py file together</strong>. 
If I try to run them from one file, <strong>either of the both starts and other one starts only when first one is done working.</strong></p>

<p><strong>How can I run 2 servers</strong> in python together?</p>

<pre><code>import thread

def run_app1():
    #something goes here

def run_app2():
    #something goes here


if __name__=='__main__':
    thread.start_new_thread(run_app1)
    thread.start_new_thread(run_app2)
</code></pre>

<p>if you need to pass args to the functions you can do:</p>

<pre><code>thread.start_new_thread(run_app1, (arg1,arg2,....))
</code></pre>

<p>if you want more control in your threads you could go:</p>

<pre><code>import threading
def app1():
    #something here

def app2():
    #something here

if __name__=='__main__':
    t1 = threading.Thread(target=app1)
    t2 = threading.Thread(target=app2)
    t1.start()
    t2.start()
</code></pre>

<p>if you need to pass args you can go:</p>

<pre><code>t1 = threading.Thread(target=app1, args=(arg1,arg2,arg3.....))
</code></pre>

<p>What's the differences between thread vs threading? Threading is higher level module than thread and in 3.x thread got renamed to _thread... more info here: <a href="http://docs.python.org/library/threading.html" rel="nofollow">http://docs.python.org/library/threading.html</a> but that's for another question I guess. </p>

<p>So in your case, just make a function that runs the first script, and the second script, and just spawn threads to run them.</p>

python python-2.7 web.py yowsup