I'm attempting to write a game server and I need it to continue on with the code while listening for a connection. I know this is done with the threading module but I don't know how exactly to do this. Python documentation didn't work for me.

#[LISTENER]
    print "Initializing Listener..."
    Listener.Listen(server_ip, int(server_port))

    #(AFTER LISTENER HAS STARTED WAITING FOR A CONNECTION)
    os.system("cls")

    print "Server IP: %s"  % server_ip + ":" + server_port
    print "Level Name: %s" % level_name
    print "Public IP: %s"  % public_ip
    print "-------------------------------------------"

    os.system("pause")

Listener code

import os, socket
import Interpreter

class Listen:
    def __init__(self, server_ip, server_port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((socket.gethostname(), server_port))
        s.listen(5)

        while 1:
            (clientsocket, address) = s.accept()
            if clientsocket and address:
                Interpreter.newConnection(clientsocket, address)

Edited 6 Years Ago by WildBamaBoy: n/a

Maybe this thread has a bad name lol...
While the server is listening for a connection it needs to continue on with the code.

#[LISTENER]
    print "Initializing Listener..."
    Listener.Listen(server_ip, int(server_port))

    #[Run this code after Listener is waiting for a connection] 
    os.system("cls")

    print "Server IP: %s"  % server_ip + ":" + server_port
    print "Level Name: %s" % level_name
    print "Public IP: %s"  % public_ip
    print "-------------------------------------------"

    os.system("pause")

But it will never get to that part after calling the Listener, because it is stuck in a while loop listening for connections.

Edited 6 Years Ago by WildBamaBoy: n/a

You can run the listening function on an asynchronized thread.

from functools import wraps
from threading import Thread

def async(func):
    @wraps(func)
    def async_func(*args, **kwargs):
        func_handler = Thread(target=func, args=args, kwargs=kwargs)
        func_handler.start()
        return func_handler
    return async_func

@async
def listener():
    print "Initializing Listener..."
    Listener.Listen(server_ip, int(server_port))

os.system("cls")

print "Server IP: %s"  % server_ip + ":" + server_port
print "Level Name: %s" % level_name
print "Public IP: %s"  % public_ip
print "-------------------------------------------"

os.system("pause")

Cheers and Happy coding

I had given up on this now I'm trying again.
Your code works fine, but could someone please explain the code to me?

def async(func):
    @wraps(func)  #Purpose of this? @ sign?
    def async_func(*args, **kwargs):
        func_handler = Thread(target=func, args=args, kwargs=kwargs)
        func_handler.start()
        return func_handler
    return async_func

@async #And this? What does the @ sign mean?
def listener():
    print "Initializing Listener..."
    Listener.Listen(server_ip, int(server_port))

The @ symbol is syntax indicating a decorator. In this instance, the decorator takes the function it decorates (a.k.a. wraps) and makes a new function that starts the old function in a new thread. A normal decorator will not preserve the name and docstring of the old function, but @wraps will preserve that information in the new function.

Once the async decorator has been defined, you can decorate any function with it to have that function start in a new thread when called.

This article has been dead for over six months. Start a new discussion instead.