Introduction
============
PyF.Station is a protocol with client and server to transfer python generators accross tcp networks. Items in the generator must be pyf.transport.Packet instances.
Best practice is to provide information about the flow in the first packet, identified as an header (containing for example authentication data, method, target, and so on).
Errors are passed on both ends.
Server
------
Please note that the server requires tgscheduler (to spawn tasks, passing generators) and twisted.
An example::
from twisted.internet import reactor
from pyf.station import FlowServer
def sample_handler(flow, client=None):
header = flow.next()
print header
for i, item in enumerate(flow):
if not i%50:
print i, item
print "end of flow..."
client.success("Done")
factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
reactor.run()
The handler can be a simple callable, as in the example above, or a user defined class:
from twisted.internet import reactor
from pyf.station import FlowServer
class SampleHandler(object):
def __init__(self, flow, client):
# The __init__ method must accept 'flow' and 'client' params
self.flow = flow
self.client = client
def handle_data(self):
# This method will be called by the FlowServer
header = self.flow.next()
print header
for i, item in enumerate(self.flow):
if not i%50:
print i, item
print "end of flow..."
self.client.success("Done")
factory = FlowServer(SampleHandler)
reactor.listenTCP(8000,factory)
reactor.run()
Another example, if you are in an already threaded env (like a wsgi server)::
from tgscheduler import scheduler
from twisted.internet import reactor
from pyf.station import FlowServer
from pyf.transport import Packet
def sample_handler(flow, client=None):
header = flow.next()
print header
for i, item in enumerate(flow):
# every 50 items...
if not i%50:
print i, item
# we send a message to the client
client.message(Packet({'type': 'info',
'message': 'hello ! (%s)' % i}))
print "end of flow..."
client.success("Done")
factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
scheduler.add_single_task(reactor.run,
kw=dict(installSignalHandlers=0),
initialdelay=0)
# You cloud comment these lines if you were in wsgi application
scheduler.start_scheduler()
while True:
pass
Client
------
Example of client::
from pyf.station import StationClient
from pyf.transport import Packet
client = StationClient('127.0.0.1', 8000, True)
def message_handler(message_packet):
# the handler for messages that come back from the server
print "Message handler triggered: %s" % message_packet
# we register our callback
client.add_listener('message_received', message_handler)
# we generate sample packets
flow = (Packet(dict(Field1=i+1,
Field2=('titi', 'tata')[i%2], num=i+1,
Field3=(i+1)*10))
for i in range(10000))
values = client.call(
flow,
header=dict(authtkt='my false auth token :)',
action='my_action'))
# here values is either "True" (saying that message has passed well) or a packet, comming back from the server.
for i, value in enumerate(values):
if not i % 5000:
print i
if isinstance(value, Packet):
print value
============
PyF.Station is a protocol with client and server to transfer python generators accross tcp networks. Items in the generator must be pyf.transport.Packet instances.
Best practice is to provide information about the flow in the first packet, identified as an header (containing for example authentication data, method, target, and so on).
Errors are passed on both ends.
Server
------
Please note that the server requires tgscheduler (to spawn tasks, passing generators) and twisted.
An example::
from twisted.internet import reactor
from pyf.station import FlowServer
def sample_handler(flow, client=None):
header = flow.next()
print header
for i, item in enumerate(flow):
if not i%50:
print i, item
print "end of flow..."
client.success("Done")
factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
reactor.run()
The handler can be a simple callable, as in the example above, or a user defined class:
from twisted.internet import reactor
from pyf.station import FlowServer
class SampleHandler(object):
def __init__(self, flow, client):
# The __init__ method must accept 'flow' and 'client' params
self.flow = flow
self.client = client
def handle_data(self):
# This method will be called by the FlowServer
header = self.flow.next()
print header
for i, item in enumerate(self.flow):
if not i%50:
print i, item
print "end of flow..."
self.client.success("Done")
factory = FlowServer(SampleHandler)
reactor.listenTCP(8000,factory)
reactor.run()
Another example, if you are in an already threaded env (like a wsgi server)::
from tgscheduler import scheduler
from twisted.internet import reactor
from pyf.station import FlowServer
from pyf.transport import Packet
def sample_handler(flow, client=None):
header = flow.next()
print header
for i, item in enumerate(flow):
# every 50 items...
if not i%50:
print i, item
# we send a message to the client
client.message(Packet({'type': 'info',
'message': 'hello ! (%s)' % i}))
print "end of flow..."
client.success("Done")
factory = FlowServer(sample_handler)
reactor.listenTCP(8000,factory)
scheduler.add_single_task(reactor.run,
kw=dict(installSignalHandlers=0),
initialdelay=0)
# You cloud comment these lines if you were in wsgi application
scheduler.start_scheduler()
while True:
pass
Client
------
Example of client::
from pyf.station import StationClient
from pyf.transport import Packet
client = StationClient('127.0.0.1', 8000, True)
def message_handler(message_packet):
# the handler for messages that come back from the server
print "Message handler triggered: %s" % message_packet
# we register our callback
client.add_listener('message_received', message_handler)
# we generate sample packets
flow = (Packet(dict(Field1=i+1,
Field2=('titi', 'tata')[i%2], num=i+1,
Field3=(i+1)*10))
for i in range(10000))
values = client.call(
flow,
header=dict(authtkt='my false auth token :)',
action='my_action'))
# here values is either "True" (saying that message has passed well) or a packet, comming back from the server.
for i, value in enumerate(values):
if not i % 5000:
print i
if isinstance(value, Packet):
print value
Metadata
Release files for pyf.station 2.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distributions (sdists)
| File | Size | Uploaded | |
|---|---|---|---|
| pyf.station-2.0.5.zip | 12.8 kB | Details | |
| pyf.station-2.0.5.tar.gz | 7.0 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyf.station-2.0.5-py2.7.egg | Legacy Egg format | - | - | Details |
| pyf.station-2.0.5-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 46.6 kB
Release files / pyf.station-2.0.5.zip
| Download URL | pyf.station-2.0.5.zip |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21a326a37c1e04513fef4773d29b9f401e286c5eb9de0219a58c087ef7c2186d
|
|
BLAKE2b-256 checksum How to use checksums |
24a67e73955e9ed085d17b4470e7682e99501c166d0536a660c6f25780a65780
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyf.station-2.0.5.tar.gz
| Download URL | pyf.station-2.0.5.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45a255bc4815fb15eeac2261fc86d7163f6adb720e0d9975c4637d3f2bd06225
|
|
BLAKE2b-256 checksum How to use checksums |
96ee3fda27340faf5cd86c5765aabf9320678833e21aa6d255e83052ec6a4fa3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyf.station-2.0.5-py2.7.egg
| Download URL | pyf.station-2.0.5-py2.7.egg |
|---|---|
| Size | 16.2 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
d7b24072e4c039f04b81d9ee4760b25eff865cceaa7ca3d4537fb1c617f6b700
|
|
BLAKE2b-256 checksum How to use checksums |
31cdee17926013009df75ff773d34c295231375cb5a233ca3e48dd536367fca3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyf.station-2.0.5-py2-none-any.whl
| Download URL | pyf.station-2.0.5-py2-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
f3f68fc5cbd1db0bbea57757a1878f2f56f8ce258df48ac386c86904a8ac7dc5
|
|
BLAKE2b-256 checksum How to use checksums |
08280606e634906cc8da9f2ae7522a61f3fd73dd4b155af338a554b469fdd19f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |