You'd have to run it in different threads or use the built-in asyncore library.
Most libraries will utelize threading without you even knowing, or it will rely on asyncore which is a standard part of Python.
Here's a combination of Threading and asyncore:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import asyncore, socket
from threading import *
from time import sleep
from os import _exit
from logger import * # <- Non-standard library containing a log function
from config import * # <- Non-standard library containing settings such as "server"
class logDispatcher(Thread, asyncore.dispatcher):
def __init__(self, config=None):
self.inbuffer = ''
self.buffer = ''
self.lockedbuffer = False
self.is_writable = False
self.is_connected = False
self.exit = False
self.initated = False
asyncore.dispatcher.__init__(self)
Thread.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.connect((server, server_port))
except:
log('Could not connect to ' + server, 'LOG_SOCK')
return None
self.start()
def handle_connect_event(self):
self.is_connected = True
def handle_connect(self):
self.is_connected = True
log('Connected to ' + str(server), 'LOG_SOCK')
def handle_close(self):
self.is_connected = False
self.close()
def handle_read(self):
data = self.recv(8192)
while self.lockedbuffer:
sleep(0.01)
self.inbuffer += data
def handle_write(self):
while self.is_writable:
sent = self.send(self.buffer)
sleep(1)
self.buffer = self.buffer[sent:]
if len(self.buffer) <= 0:
self.is_writable = False
sleep(0.01)
def _send(self, what):
self.buffer += what + '\r\n'
self.is_writable = True
def run(self):
self._send('GET / HTTP/1.1\r\n')
while 1:
logDispatcher() # <- Initate one for each request.
asyncore.loop(0.1)
log('All threads are done, next loop in 10', 'CORE')
sleep(10)
Or you could simply do a thread that does the job and then dies.
from threading import *
class worker(Thread):
def __init__(self, host, postdata)
Thread.__init__(self)
self.host = host
self.postdata = postdata
self.start()
def run(self):
sock.send(self.postdata) #Pseudo, create the socket!
for data in postDataObjects:
worker('example.com', data)
If you need to limit the number of threads (if you're sending over 5k posts or so it might get taxing on the system) just do a while len(enumerate()) > 1000: sleep(0.1)
and let the looper object wait for a few threads to die out.