473,795 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with concurrency control (threads/processes & signals)

Hi,

I am working on an application which involves
interprocess communication. More to the point,
processes should be able to notify other processes
about certain situations, so the "notifyees" would be
able to act in certain ways.

As processes are distributed on several machines, in
different physical locations, my thinking was:
a) set a message manager (MM)
b) all the participants will register with MM, so MM
will have their host address and their pid on host
c) when someone needs to send a notification, it is
sent to MM, and MM it's doing the job

To start with, I created processes, psrv.py and
clnt.py, and tried to sketch the above scenario.

Here is the server, psrv.py:
#! /usr/bin/python

from socket import *
from os import *
from signal import *
from cPickle import *
#import pdb
import thread

#pdb.set_trace( )

def process(ms):
global c,regtab,mssgs
print 'ms[0]=',ms[0]
if ms[0]=='0':
regtab.append(m s[1])
mssgs.append([])
elif ms[0]=='1':
for i in range(len(regta b)):
mssgs[i].append(ms[1])
kill(regtab[i],SIGUSR1)
elif ms[0]=='2':
for i in range(len(regta b)):
if regtab[i]!=ms[2]:
mssgs[i].append(ms[1])
kill(regtab[i],SIGUSR1)
elif ms[0]=='3':
print 'executing ms[0]==3'
for i in range(len(regta b)):
if regtab[i]==ms[1]:
c,p=s.accept()
for j in mssgs[i]:
c.send(dumps(j, True))
c.close()
print 'connection closed'

regtab=[]
mssgs=[]

s=socket()
s.bind(('127.0. 0.1',9691))
s.listen(5)

while True:
print 'listening ...'
c,p=s.accept()
while True:
m=c.recv(1024)
if not m: break
ms=loads(m)
c.close()

thread.start_ne w_thread(proces s,(ms,))
# *************** *************** *************** ******

And here is the client side, clnt.py:
#! /usr/bin/python

from socket import *
from os import *
from signal import *
from cPickle import *
#import pdb

#pdb.set_trace( )

def k3(a,b):
print 'Executing k3'
s=socket()
s.connect(('127 .0.0.1',9691))
s.send(dumps((' 3',getpid()),Tr ue))
while True:
print '"3" sent'
m1=s.recv(1024)
print m1
if not m1: break
m=loads(m1)
s.close()
print 'connection closed in k3'
print 'm is ',m

signal(SIGUSR1, k3)

m=''
k=raw_input()
while k!='4':
s=socket()
s.connect(('127 .0.0.1',9691))
if k=='0':
s.send(dumps((' 0',getpid()),Tr ue))
elif k=='1':
s.send(dumps((' 1','For all'),True))
elif k=='2':
s.send(dumps((' 2','For some',getpid()) ,True))
elif k=='3':
s.send(dumps((' 3',getpid()),Tr ue))
while True:
m1=s.recv(1024)
if not m1: break
m=loads(m1)
s.close()
print 'connection closed in main'
print 'm is',m
k=raw_input()
# *************** *************** *************** ******
Comments: Clients register themselves with server by
sending a message of type '0'. If they want to
broadcast a message, clients will ask nicely the
server to do it for them through a message of type
'1'. The message is supposed to be kept in
eachclient's "mailbox", then the server should notify
each client by sending a SIGUSR1 signal, and then
waiting for each clientto ask for its own copy of the
notification. When a client is ready to do it, will
just send a message of type '3' to the server.
Messages of type '2' are just a variation of type '1'
which dosen't include the sender in the list of
receivers.

My first version was forking a new process in the
server to serve requests, but I soon realized that, as
each process receives a copy of the original data,
updating the "mailboxes" won't work (the "mailboxes"
updated are local copies). In the threads version the
"mailboxes" are shared, but so are the sockets, which
will force me to add some locks and make sure that an
open socket in the server will talk only with a
certain client.

For the moment I am toying with one server, and one
client. Still, it is not working as expected. '0' goes
through, so does'1', but not '3' ('3' as a result of
'1', or '3' requested from keyboard).

Life is a struggle. Programming in Python shouldn't
be. Ergo, I'm doing something wrong.

Any advice?

Thanks,
Sorin

Environment: Gentoo Linux, Python 2.4.1

_______________ _______________ ____
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
Oct 27 '05 #1
1 1862
[Sori Schwimmer]
I am working on an application which involves
interprocess communication. More to the point,
processes should be able to notify other processes
about certain situations, so the "notifyees" would be
able to act in certain ways.

As processes are distributed on several machines, in
different physical locations, my thinking was:
a) set a message manager (MM)
b) all the participants will register with MM, so MM
will have their host address and their pid on host
c) when someone needs to send a notification, it is
sent to MM, and MM it's doing the job
[snip]
Life is a struggle. Programming in Python shouldn't
be. Ergo, I'm doing something wrong.

Any advice?


Rather than rolling your own, have you considered using the spread
module: robust, tested, efficient and no infrastructure development
required.

http://www.zope.org/Members/tim_one/spread/
http://www.python.org/other/spread/

The latter page has links to the original C spread module, which has
documentation, FAQs, etc.

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Oct 27 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
26652
by: Derek Fountain | last post by:
Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run this bit of code" sort of thing?
2
2056
by: Holger Joukl | last post by:
Hi, migrating from good old python 1.5.2 to python 2.3, I have a problem running a program that features some threads which execute calls to an extension module. Problem is that all of a sudden, I cannot stop the program with a keyboard interrupt any more; the installed signal handler does not seem to receive the signal at all. This happens both if I rebuild this extension using python 2.3 headers/library and if I simply use the old...
9
1878
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but since I'm asking in the first place, assume that assumption to be very assuming. thanks, Eric
9
2036
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more pythonic approach. One thing I've been seeing suggested a lot lately is that running jobs in separate processes, to make it easy to use the latest multiprocessor machines. Makes a lot of sense to me, those processors are going to be more and...
23
6101
by: Simon Wittber | last post by:
I've just bought a new notebook, which has a dual core CPU. I write cross platform games in Python, and I'd really like to be able to use this second core (on my machine, and on user's machines) for any new games I might write. I know threads won't help (in CPython at least) so I'm investigating other types of concurrency which I might be able to use. I really like the PyLinda approach, however I need to be able to pass around all the...
5
1876
by: Ark | last post by:
Consider static T foo; .............. T get_foo(void) { return foo; } In a multi-threaded (or -tasked) environment, I need to ensure that I get_foo() grabs T atomically (e.g. is not preempted while accessing foo). Are there types T for which atomic fetch /guaranteed/ portably? Does the
0
1009
by: Mitko Haralanov | last post by:
Hi everyone, First off, I know that this has been discussed before and I did a search but could not find anything that helped my situation. Here is the problem: I have a Python program that uses threads, forked processes, and signals and I can't seem to understand where the signals go. When the program starts, it creates a thread, which spins in select
15
2584
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
8
1466
by: Amar | last post by:
I am a system programmer , doing my program in C/RedHat LINUX ES4.0 version. I want to do concurrent programming in C , which will utilise my system's 4 processor simutaneously. I need some one's help to proceed. Looking for early reply. Amartya
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.