Re: Pause a thread/ execfile()

2009-10-26 Thread Saju Pillai

On 26/10/09 12:28 PM, Babloo wrote:

i have a small python application with GUI (frontend) which has
various functions. I have a RUN button which runs python scripts in
the background . It basically calls execfile() function internally
which runs in a thread ,  to run the python script .

I want to implement a PAUSE feature which would pause the running
python script . So do that i have to either pause the thread in which
execfile() runs or pause execfile itself .

When the user presses RUN again then the python script should resume
running .


Any ideas how to pause the thread / pause execfile() . Any other ideas
for the same would be helpful .


Other ideas ? You could use job control signals if you are on unix. Try
forking a child process instead of using a thread. Sending SIGSTOP to
the forked child will stop/pause it, SIGCONT will resume it.

-srp
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to declare python ints in C extensions?

2009-01-04 Thread Saju Pillai

Tony Houghton wrote:

I want to write python wrappers for the Linux DVB API. The underlying
structures and constants may change from time to time, and some of the
constants are generated from macros, so I think it would be better to
write the module in C rather than just copying the constants into pure
python code and using python's ioctl and struct modules.

The trouble is I can't find out how to define a simple python int
variable/constant in a C extension. The docs only seem to tell you how
to define functions/methods and new types.

For example, where the kernel header dvb/frontend.h defines:

typedef enum fe_type {
FE_QPSK,
FE_QAM,
FE_OFDM,
FE_ATSC
} fe_type_t;

I want to make them available as if there was a python module
dvb/frontend.py containing:

FE_QPSK = 0
FE_QAM = 1
FE_OFDM = 2
FE_ATSC = 3

but in real life the module would be dvb/frontendmodule.c.



Create Python Int objects for each of these constants and set them into 
your module object's dictionary


foo = PyInt_FromLong(1L);
PyDict_SetItemString(PyModule_GetDict(your_module), foo, foo);
Py_DECREF(foo)


srp
--
http://saju.net.in
--
http://mail.python.org/mailman/listinfo/python-list


Re: select.select and socket.setblocking

2009-01-03 Thread Saju Pillai

Bryan Olson fakeaddr...@nowhere.org wrote:


Where does this come up? Suppose that to take advantage of multi-core 
processors, our server runs as four processes, each with a single thread 
that responds to events via select(). Clients all connect to the same 
server port, so the socket listening on that port is shared by all four 
processes. A perfectly reasonable architecture (though with many more 
processes the simple implementation suffers the thundering herd problem).


Which is why it is common for real world servers to serialize the
select()/accept() code - usually via a file lock or a semaphore.
-srp 
-- 
http://saju.net.in


Two of our processors may be waiting on select() when a new connections 
comes in. The select() call returns in both processes, showing the 
socket ready for read, so both call accept() to complete the connection. 
  The O.S. ensures that accept() [and recv()] are atomic, so one process 
gets the new connection; what happens in the other depends on whether we 
use a blocking or non-blocking socket, and clearly we want non-blocking.


-- 
--Bryan





--
http://mail.python.org/mailman/listinfo/python-list


Re: select.select and socket.setblocking

2008-12-31 Thread Saju Pillai
On Dec 31, 2:01 pm, Francesco Bochicchio bock...@virgilio.it wrote:
 Grant Edwards ha scritto:

  On 2008-12-30, Francesco Bochicchio bock...@virgilio.it wrote:

  3. AFAIK (sorry, I feel acronym-ly today ;), there is no difference in
  select between blocking and non-blocking mode. The difference is in the
  recv (again, assuming that you use TCP as protocol, that is AF_INET,
  SOCK_STREAM), which in the blocking case would wait to receive all the
  bytes that you requested,

  No, in blocking mode it will wait to receive _some_ data (1 or
  more bytes).  The requested amount is strictly an upper
  limit: recv won't return more than the requested number of
  bytes, but it might return less.

 Uhm. In my experience, with TCP protocol recv only returned less than
 the required bytes if the remote end disconnects. I always check the

What if the sending end actually sent less than you asked for ?

-srp

 returned value of recv and signal an error if the read bytes are less
 than the expected ones, but this error is never occurred (and its about
 20 years that I use sockets in various languages and various flavor of
 unix and occasionally on windows. Maybe  have always been lucky ? :-)

 And, on some unices  system call recv also returns when a signal
 interrupts the syscall, but I half-remember reading that python recv in
 such a case repeat the system call by itself ... although this might be
 only my desire ...

  In non-blocking mode, it will always return immediately, either
  with some data, no data (other end closed), or an EAGAIN or
  EWOULDBLOCK error (I forget which).

  [...] I myself tend to avoid using non-blocking sockets, since
  blocking sockets are much easier to handle...

  That depends on whether you can tolerate blocking or not.  In
  an event-loop, blocking is generally not allowed.

 What I usually do, when I cannot block is:

 - use socket in blocking mode
 - do a select with a very small timeout and do a recv only if the select
 returns with input events
 - (with TCP) do a recv for the exact amount of bytes that I expect (
 this mean having a user protocol that carries the message size in the
 header, but this is usually the case ).

 This usually worked for me.

 If my process (or thread) has only to deal with socket I/O, I make a
 blocking select, and then make an 'exact' recv on whichever socket the
 select signals.

 Ciao
 
 FB

--
http://mail.python.org/mailman/listinfo/python-list


Re: select.select and socket.setblocking

2008-12-31 Thread Saju Pillai
On Dec 31, 7:48 pm, Francesco Bochicchio bock...@virgilio.it wrote:
  ... 

  Uhm. In my experience, with TCP protocol recv only returned less than
  the required bytes if the remote end disconnects. I always check the

  What if the sending end actually sent less than you asked for ?

  -srp

 In blocking mode and with TCP protocol, the recv waits until more bytes
 are received -  mixing up the next message with the previous one and

Is this correct ? IIRC even in blocking mode recv() can return with
less bytes than requested, unless the MSG_WAITALL flag is supplied.
Blocking mode only guarantees that recv() will wait for a message if
none is available - but not that it *will* return the number of bytes
requested.

-srp

 then loosing the 'sync' and being unable to interpretate the received
 data -  or the remote end disconnects.

 Yes this is bad,  and is a good reason why socket receive should be
 handled   in non-blocking mode if you receive data from untrusted
 sources. But luckily for me, as I said in the other post, I used socket
 mostly to communicate between specific applications on a private LAN or
 WAN, so I could afford to ignore the problem.

 Ciao
 
 FB

--
http://mail.python.org/mailman/listinfo/python-list


Re: Can anyone suggest a good HTTP/1.1 web client?

2008-12-16 Thread Saju Pillai
On Dec 17, 1:18 am, Kottiyath n.kottiy...@gmail.com wrote:
 Hi all,
     I have to connect to a secure website every second to get the data
 and then post to it. I have been investigating on many web clients in
 python, but nothing fits the bill properly.
     The ones I tried implementing are:
     1. httplib based - I created myself. (I cannot use urllib2 since I
 have to transfer files, and urllib2 doesnt have multipart content-type
 support)
     2. Twisted web client.
     I also looked at mechanize etc too.

     The problems I face are -
     1. I liked twisted a lot, but when I implemented it, I found that
 client support is there only for twisted.web and not twisted.web2.
 Since I connect to the same website every time, I would like to have
 persistent connections and since twisted.web is HTTP/1.0, persistent
 connection support is not yet there. Without persistent connections, I
 would have to have TCP connection handshake everytime and it is taking
 too much time.
     2. Since I connect to the website every second, I have to have
 many connections running at the same time. I am worried that creating
 threads for each connection is going to be a big problem (esp if the
 server response is slow), since the processor will get swamped -
 especially since there are many other activities going on in the
 machine.
    3. I would also like to pipe line the requests - esp if the
 response is slow.

    Other requirements:
    1. HTTPS Support
    2. Connection through proxy.


You could take a look at pycurl - python bindings for libcurl.
http://pycurl.sourceforge.net/

srp


    Is there any good web client which I can use straight up? Or would
 I have to implement the whole thing myself? It looks like a big beast
 and I was wondering whether python provides it straight up.

 Regards
 K

--
http://mail.python.org/mailman/listinfo/python-list


Re: Python, threading

2008-12-11 Thread Saju Pillai
On Dec 11, 6:06 pm, SMALLp [EMAIL PROTECTED] wrote:
 Hy. I have a problem! I'm making multi thread application (client,
 server) using wxPython for GUI, and threading.Thread for threding.

 Clients connect and when they are connected (evry thread handles one
 connection) threads change main window.

 I neded tip how to make communication between threeds.

Threads already share data your problem would likely be to synchronize
the threads - threading.Sempahore  threading.Lock will help
Maybe you want some threads to wait while other thread(s) do some
work ? - threading.Event  threading.Condition

The documentation on threading module is where you should start.

-srp
--
http://saju.net.in
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert uint64 in C into Python 32bit Object [ I am using Python2.2 ]

2008-12-11 Thread Saju Pillai
On Dec 11, 6:45 pm, Explore_Imagination [EMAIL PROTECTED]
wrote:
 On Dec 11, 4:45 am, John Machin [EMAIL PROTECTED] wrote:



  On Dec 11, 9:49 am, Explore_Imagination [EMAIL PROTECTED]
  wrote:

   Hi all

   I am new to C and python ... I want to convert C data type uint64
   variable into the Python 32bit Object. I am currently using Python 2.2
   [ It is necessary to use it ]

   Kindly give your suggestion how and in which way I can achieve this
   task.

  I'm not sure what you mean by the Python 32bit Object. A Python int
  object holds a signed 32-bit integer. A Python long object holds a
  signed integer of arbitrary size. You will need to convert your uint64
  into a Python long; then, if necessary, check that the result will fit
  in an int (result = sys.maxint).

  If the C variable is in an 8-byte string that you have read from a
  file, the unpack function in the struct module will do the job.
  Assuming your computer is little-endian:

   maxu64 = '\xff' * 8 # example input string
   import struct
   result = struct.unpack('Q', maxu64)[0]
   result

  18446744073709551615L 2 ** 64 - 1

  18446744073709551615L

  If however you mean that in C code you need to build a Python object
  to pass over to Python code: According to the Python/C API Reference
  Manual (http://www.python.org/doc/2.2.3/api/longObjects.html):

  PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
      Return value: New reference.
      Returns a new PyLongObject object from a C unsigned long long, or
  NULL on failure.

  If however you mean something else, 

  HTH,
  John

 Thanks for your feedback ... Actually I want to pass unit64 variable
 in C to python
 but at the same time I want to have a generic code which should work
 on both little-endian
 and big endian architectures

 Any suggestions ?

I am not sure if endianness comes into the picture. As long as sizeof
(uint64) == sizeof(unsigned long long) on your platform, python should
have no problem accepting the return value of
PyLong_FromUnsignedLongLong(uint64_var);

srp
--
http://saju.net.in
--
http://mail.python.org/mailman/listinfo/python-list


Re: Any way to loop through object variables?

2008-05-28 Thread Saju Pillai


On 28-May-08, at 9:49 PM, Gary Herron wrote:


Dave Challis wrote:

Hi,
Just wondering if there's a way to iterate through all variables  
which

an object has set?

Specifically, I'm using the OptionParser module, which returns an
options object, containing all command line options as object  
variables.

I'd like to iterate over these rather than getting at them by name.

Cheers,
Dave


For this object (and many others), you can get at the attributes  
with the vars(...) builtin.

It returns a dictionary of attribute names and values.



Also dir(object) will get you the list of attributes of that object.

-srp




Gary Herron


--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list