我们正在移植建模应用程序,它在建模过程中使用IronPython脚本进行自定义操作。现有的应用程序在单独的线程中执行每个Python脚本,并为此使用协作模型。现在我们想把它移植到TPL,但首先我们要测量上下文切换 . 基本上,我们现在拥有的是:
Task
s队列Task
都执行一个IronPython脚本Task
(IronPython执行)转移到等待状态我们要做的是:
Task
的队列Task
时,我们尝试执行它Task
时,我们检查它是否处于等待状态。如果是这样的话,我们就把它叫醒,然后试着去执行。在Task
Task
我真的不知道这是合作多任务处理吗?
我们正在考虑定制TaskScheduler
,这是个好方法吗?或者有人知道更好的解决方案吗?在
谢谢。在
更新时间:
好的,例如,我有这样的代码:
public class CooperativeScheduler : TaskScheduler, IDisposable
{
private BlockingCollection<Task> _tasks;
private Thread _thread;
private Task _currentTask;
public CooperativeScheduler()
{
this._tasks = new BlockingCollection<Task>();
this._thread = new Thread(() =>
{
foreach (Task task in this._tasks.GetConsumingEnumerable())
{
this._currentTask = task;
TryExecuteTask(this._currentTask);
}
}
);
this._thread.Name = "Cooperative scheduler thread";
this._thread.Start();
}
public void SleepCurrentTask()
{
if (this._currentTask != null)
{
// what to do here?
}
}
protected override IEnumerable<Task> GetScheduledTasks()
{
return this._tasks.ToArray<Task>();
}
protected override void QueueTask(Task task)
{
// No long task
this._tasks.Add(task);
}
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
throw new NotImplementedException();
}
public void Dispose()
{
this._tasks.CompleteAdding();
this._thread.Join();
}
}
自定义任务调度器,它有一个用于任务执行的_thread
和用于运行任务的_currentTask
字段,而且它还有{
客户端代码很简单:
^{pr2}$也许有人有更好的主意?在
听起来你应该使用producer/consumer模式。在
在这个免费的PDF文件中查看第55页,Patterns of Parallel Programming
相关问题 更多 >
编程相关推荐