signals

Django 1.2: How to connect pre_save signal to class method

旧时模样 提交于 2019-12-01 04:11:16
I am trying to define a "before_save" method in certain classes in my django 1.2 project. I'm having trouble connecting the signal to the class method in models.py. class MyClass(models.Model): .... def before_save(self, sender, instance, *args, **kwargs): self.test_field = "It worked" I've tried putting pre_save.connect(before_save, sender='self') in 'MyClass' itself, but nothing happens. I've also tried putting it at the bottom of the models.py file: pre_save.connect(MyClass.before_save, sender=MyClass) I read about connecting signals to class methods here , but can't figure out the code.

MySQL exception handler access exception being handled

半腔热情 提交于 2019-12-01 04:08:57
I'm trying to rollback on an error, but still let the client receive the error . This might actually be impossible, unless there is a way to access the error in an exception handler. It's possible to "throw" from an exception, i.e. it's possible to raise a signal : CREATE PROCEDURE p () BEGIN DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN SIGNAL SQLSTATE VALUE '99999' SET MESSAGE_TEXT = 'An error occurred'; END; DROP TABLE no_such_table; END; But this sample code from the MySQL doc looks horrible , because it literally swallows all errors and jams them into one. SHOW ERRORS seems relevant, but I

What is the use of ignoring `SIGCHLD` signal with `sigaction(2)`?

走远了吗. 提交于 2019-12-01 03:56:52
问题 It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't wait() for it to _exit() ) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD is ignored by default anyway. How come does this work? int main (void) { struct sigaction sa; sa.sa_handler = SIG_IGN; //handle signal by ignoring sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) == -1) { perror(0); exit(1); } int pid = fork

Use flock() in the sigaction handler

妖精的绣舞 提交于 2019-12-01 03:51:31
问题 flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all. Is it possible to use flock() in the sigaction handler without problems? 回答1: According to @alk answer in the following topic: We can develop our property flock() function (its name could be async_flock() ). we copy in this new function the content of the origin lockf() code

How can I override the keyboard interrupt? (Python)

大城市里の小女人 提交于 2019-12-01 03:49:07
Is there anyway I can make my script execute one of my functions when Ctrl+c is hit when the script is running? Take a look at signal handlers . CTRL-C corresponds to SIGINT (signal #2 on posix systems). Example: #!/usr/bin/env python import signal import sys def signal_handler(signal, frame): print 'You pressed Ctrl+C - or killed me with -2' sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print 'Press Ctrl+C' signal.pause() Sure. try: # Your normal block of code except KeyboardInterrupt: # Your code which is executed when CTRL+C is pressed. finally: # Your code which is always

Linux: handling a segmentation fault and getting a core dump

一世执手 提交于 2019-12-01 03:44:06
When my application crashes with a segmentation fault I'd like to get a core dump from the system. I do that by configuring before hand ulimit -c unlimited I would also like to have an indication in my application logs that a segmentation fault has occured. I do that by using sigaction() . If I do that however, the signal does not reach its default handling and a core dump is not saved. How can I have both the system core dump an a log line from my own signal handler at the same time? Overwrite the default signal handler for SIGSEGV to call your custom logging function. After it is logged,

sem_post, signal handlers, and undefined behavior

爱⌒轻易说出口 提交于 2019-12-01 03:35:57
问题 Does this use of sem_post() in a signal handler rely on undefined behavior? /* * excerpted from the 2017-09-15 Linux man page for sem_wait(3) * http://man7.org/linux/man-pages/man3/sem_wait.3.html */ ... sem_t sem; ... static void handler(int sig) { write(STDOUT_FILENO, "sem_post() from handler\n", 24); if (sem_post(&sem) == -1) { write(STDERR_FILENO, "sem_post() failed\n", 18); _exit(EXIT_FAILURE); } } The semaphore sem has static storage duration. While the call to sem_post () is async

signal.alarm function with resolution greater than 1 second?

你离开我真会死。 提交于 2019-12-01 03:34:12
I'm trying to build a python timeout exception that runs in milliseconds. The python signal.alarm function has a 1 second resolution. How would one get an equivalent function that requests a SIGALRM signal to a given process in, say milliseconds, as opposed to seconds? I've found no simple solutions as of yet. Thanks in advance for your input. Use signal.setitimer() instead. 来源: https://stackoverflow.com/questions/3770711/signal-alarm-function-with-resolution-greater-than-1-second

EventHandler type with no event args

末鹿安然 提交于 2019-12-01 03:18:28
When we want to pass data to an event subscriber, we use EventArgs (or CustomEventArgs) for this. .Net provides a build in type EventHandler that uses as a parameter an instance of EventArgs class that is build in as well. What about cases, when I need to notify a subscriber that some action is over, for example search is over? I don't want to even use EventArgs, that won't contain anything. Is there a build in type for signaling another class, without the need to use empty EventArgs? Carsten You can do a couple of things: Use your normal event with EventHandler and the basic EventArg class -

Django signal m2m_changed not triggered

牧云@^-^@ 提交于 2019-12-01 02:57:46
I recently started to use signals in my Django project (v. 1.3) and they all work fine except that I just can't figure out why the m2m_changed signal never gets triggered on my model. The Section instance is edited by adding/deleting PageChild inline instances on an django admin form. I tried to register the callback function either way as described in the documentation, but don't get any result. Excerpt from my models.py from django.db import models from django.db.models.signals import m2m_changed class Section(models.Model): name = models.CharField(unique = True, max_length = 100) pages =