避免在Matlab GUI中中断回调函数(Avoid interruption of callback functions in Matlab GUI)
我有一个Matlab GUI,需要很长时间来执行一些回调函数。 此外,这些功能包括以下代码:
drawnow('expose'); pause(handles.data.delay);
我想避免那些回调执行被中断,以避免在用户按下其他按钮时数据不一致。 因此,我将图形设置修改为:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
但是,回调仍然中断。 我怎么能避免呢?
注意:我认为问题是我需要将'BusyAction'和'Interruptible'值传播到我GUI中的所有控件,有没有办法自动执行? 例如,在生成GUI之前修改默认值。
I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose'); pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
原文:https://stackoverflow.com/questions/14976907
最满意答案
如前所述,您可能不需要芹菜。 以下是来自案例2的示例: https : //zapier.com/blog/async-celery-example-why-and-how/ 。 它完全为我工作:
from time import sleep import json from django.http import HttpResponse from django.shortcuts import render def main_view(request): return render(request, 'index.html') def ajax_view(request): sleep(10) #This is whatever work you need pi1 = "This is pi1" #I just made pi1/pis1 random values pis1 = "This is pis1" context = { "pi1" : pi1, "pis1" : pis1, } data = json.dumps(context) return HttpResponse(data, content_type='application/json')
我的index.html包含:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Main View</title> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function(){ $.ajax({ url: "/test_ajax/", }).done(function( data) { $("#pi1").text(data.pi1); $("#pis1").text(data.pis1); }); }); </script> </head> <body> <h1 id = "pi1">Loading</h1> <h1 id = "pis1">Loading</h1> </body> </html>
我的urls.py包含:
from django.conf.urls import include, url from django.contrib import admin from testDjango.test import main_view, ajax_view urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^test/', main_view), url(r'^test_ajax/', ajax_view) ]
当我访问localhost时会发生什么:8000 / test /是我立刻看到的:
大约10秒后,我看到:
这个想法是,你立即返回你的页面,并使用jquery获取操作的结果,每当完成并相应地更新你的页面。 您可以添加更多内容,例如进度条/加载图像等。例如,您可以在后台处理
pi1
和pis
,并在完成后将其加载到HTML中。As is said previous, you might not need celery. Here's an example derived from case 2 of this: https://zapier.com/blog/async-celery-example-why-and-how/. It's fully working for me:
from time import sleep import json from django.http import HttpResponse from django.shortcuts import render def main_view(request): return render(request, 'index.html') def ajax_view(request): sleep(10) #This is whatever work you need pi1 = "This is pi1" #I just made pi1/pis1 random values pis1 = "This is pis1" context = { "pi1" : pi1, "pis1" : pis1, } data = json.dumps(context) return HttpResponse(data, content_type='application/json')
My index.html contains:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Main View</title> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function(){ $.ajax({ url: "/test_ajax/", }).done(function( data) { $("#pi1").text(data.pi1); $("#pis1").text(data.pis1); }); }); </script> </head> <body> <h1 id = "pi1">Loading</h1> <h1 id = "pis1">Loading</h1> </body> </html>
And my urls.py contains:
from django.conf.urls import include, url from django.contrib import admin from testDjango.test import main_view, ajax_view urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^test/', main_view), url(r'^test_ajax/', ajax_view) ]
What happens when I visit localhost:8000/test/ is that I instantly see:
After about 10 seconds, I then see:
The idea is that you return your page instantly and use jquery to fetch the result of the operation whenever that's finished and update your page accordingly. You can add more things like progress bars/loading image etc. For your example, you can do the processing for
pi1
andpis
in the background and load it into the HTML after that's finished.
相关问答
更多-
Celery有很好的文档,也适用于那些入门的人 ,但有两个事实值得初学者提及: 使用djkombu作为BROKER_BACKEND 。 这将为您提供一个非常简单的开发消息队列,其中所有消息都存储在django使用的SQL数据库中。 由于celery的api,您可以轻松地将其替换为“真实”的消息队列进行生产: BROKER_TRANSPORT = "kombu.transport.django" Django-celery有一个设置CELERY_ALWAYS_EAGER 。 如果设置为True则不会进行异步后 ...
-
Django Celery延迟()总是推送到默认的'芹菜'队列(Django Celery delay() always pushing to default 'celery' queue)[2023-05-09]
这个问题比我想象的要简单得多 - 不正确的文档! Celery文档要求我们使用CELERY_DEFAULT_QUEUE在celery对象上设置task_default_queue配置。 参考: http : //docs.celeryproject.org/en/latest/userguide/configuration.html#new-lowercase-settings 我们当前应该使用CELERY_TASK_DEFAULT_QUEUE 。 这是所有其他设置名称命名的不一致。 它是在Github上提 ... -
Django Celery任务排队(Django Celery tasks queue)[2022-08-29]
你可以限制工作人员的任务,我假设你只需要一个工人,所以在调用djcelery时只需要启动一个工人。 python manage.py celery worker -B --concurrency=1 You can limit the tasks workers, in your cause i assume you just need one worker at time, so just start one worker when calling the djcelery. python manage ... -
错误的任务名称 。 通过将任务修饰器更改为@celery_app1.task(name='tasks.rank_all')并调整我的节拍计划以包含正确的名称来解决此问题: CELERYBEAT_SCHEDULE = { 'tasks.rank_all': { 'task': 'tasks.rank_all', 'schedule': timedelta(seconds=30), }, } Wrong task name. This got fixed by ...
-
芹菜与redis后端(celery with redis backend)[2021-08-23]
你有import djcelery; djcelery.setup_loader() import djcelery; djcelery.setup_loader()在你的settings.py ? 您可以通过运行以下命令找出它尝试连接的位置: $ python manage.py celeryctl shell >>> celery.broker_connection().as_uri() 'setup_loader'非常重要,所以你必须确保你包含它。 (好消息是,芹菜将支持版本2.7的Djang ... -
Django - 如何使用芹菜和redis的异步任务队列(Django - How to use asynchronous task queue with celery and redis)[2023-09-04]
如前所述,您可能不需要芹菜。 以下是来自案例2的示例: https : //zapier.com/blog/async-celery-example-why-and-how/ 。 它完全为我工作: from time import sleep import json from django.http import HttpResponse from django.shortcuts import render def main_view(request): return render(reques ... -
来自http://celery.readthedocs.org/en/latest/getting-started/brokers/redis.html : 监视事件(由花和其他工具使用)是全局的,不受虚拟主机设置的影响。 这是由Redis的限制引起的。 Redis PUB / SUB通道是全局的,不受数据库编号的影响。 这似乎是Redis的一个警告:( From http://celery.readthedocs.org/en/latest/getting-started/brokers/redis.ht ...
-
该错误告诉您它无法序列化整个request以将其发送到任务。 但是,您在请求中使用的唯一事物是用户对象。 您应该只发送用户ID,并在任务中获取对象本身。 export_cases_palu.delay(user_id=request.user.id) ... def export_cases_palu(user_id): user = User.objects.get(id=user_id) d = Context({'username': user.username}) (另外,你不 ...
-
我解决了这个问题,但我不确定如何。 第二天,我回到了这个确切的配置,任务正在向芹菜工人提出。 也许我重启的服务之一就是关键,但我不确定。 如果有其他人遇到此问题,尤其是在Windows上:确保您的redis-server处于活动状态,并且您可以看到来自ping和任务的传入连接。 在发布此问题之前我已经这样做了,但似乎可能是错误配置的候选人。 I got around this, but I'm not sure how. I came back to this exact configuration the ...
-
Celery中的周期性任务非常类似于此。 有一个专门的调度程序( celery beat ),只需在到期时发送任务。 您还可以通过celery.beat.Scheduler类来创建新的调度程序以用于beat ,您也可以通过celery.beat.Scheduler来创建自定义日程表(如已内置的crontab日程表)。 django-celery扩展( djcelery.schedulers.DatabaseScheduler )中有一个数据库支持的调度程序实现,它使用许多技巧来避免过于频繁地轮询数据库等等( ...