我有一个 Ada 任务,Do_Something需要“调用”自身。我认为它来自 C++,我有一个线程正在运行void Do_Something(),有时void Do_Something()需要生成更多也运行的线程void Do_Something()。
艾达(我正在学习)不喜欢这样。这似乎是一个M(非)WE:
task body A_Task is
new_task: access A_Task;
my_level: Natural;
begin
accept Do_Something(level: in Natural) do
my_level := level + 1;
end Do_Something;
if my_level < 4 then
new_task := new A_Task;
new_task.Do_Something(my_level);
end if;
end A_Task;
Run Code Online (Sandbox Code Playgroud)
GNAT 至少不喜欢这样,报告错误是new_task.Do_Something(my_level);因为
task type cannot be used as type mark within its own spec or body
Run Code Online (Sandbox Code Playgroud)
然而,通过在任务之外添加这样的过程可以很容易地规避这一点:
procedure Circumvent(level: Natural) is
new_task: access A_Task;
begin
new_task …Run Code Online (Sandbox Code Playgroud) 因此,我在 Xamarin 中有一个监视器页面,其中我需要每秒刷新一次值(从 TcpClient 获取的值)。我在 CS 文件中设置了一个公共字符串,并在 Xaml 中进行了一些基本的绑定,如下所示:<Label Text="{Binding myString}"/>
在我后面的代码中,我运行了一个异步任务来刷新myString. 该任务看起来像这样:
protected async Task syncDisplay(){
while(true){
//TcpClient going on, getting some values
myString = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我只是创建一个新线程来运行任务。就这样。问题是没有值更新到我的用户界面。当我尝试为任务外部的字符串赋值时,它工作正常。我确信我在这里遗漏了一些重要的东西,但我真的不知道是什么。
提前致谢
关于任务的 .Net 文章显示了以下两个代码片段,一个使用 wait,另一个使用 Task.Wait,并表示两者“功能等效”。
那么这在技术上是不是不正确呢?有人可以澄清一下吗?
另外,如果任务应该是异步的并构成异步编程 (TPL) 的基础,那么为什么 ASP.Net 无论如何都允许同步等待它们呢?这不会违反他们的主要用途吗?
using System;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
await Task.Run( () => {
// Just loop.
int ctr = 0;
for (ctr = 0; ctr <= 1000000; ctr++)
{}
Console.WriteLine("Finished {0} loop iterations",
ctr);
} );
}
}
// The example displays the following output:
// Finished 1000001 loop iterations
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Threading.Tasks;
public class Example
{
public static async Task …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Gulp自动化一些任务以进行 WordPress 主题开发。我已经设法让一切正常工作,除了gulp.watch不会自动监视我的文件的更改。
CLI version 2.0.1Local version 3.9.1{
"name": "theme-development-boilerplate",
"version": "1.0.0",
"description": "A boilerplate for Theme Development automated with Gulp. Includes SASS, Bootstrap, jQuery, Normalize, Mordernizer, among other stacks",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Manuel Abascal",
"license": "MIT",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-concat": "^2.6.1",
"gulp-error-notifier": "^1.1.0",
"gulp-imagemin": "^5.0.3",
"gulp-livereload": "^4.0.1",
"gulp-sass": "^4.0.2",
"gulp-uglify": "^3.0.1"
},
"dependencies": {
"browser-sync": …Run Code Online (Sandbox Code Playgroud) 我正在重构代码,该代码使用Thread.Sleep越来越长的时间限制来在出现错误时重试 SQL 查询。阻塞的常见替代Thread.Sleep似乎是await Task.Delay,这需要将方法更改为async. 该方法现在如下所示(为简洁起见,删除了额外的错误检查):
private static async Task<int> PrepareForRetry( int retryCount, SqlCommand command, int timeout )
{
++retryCount;
if (retryCount < ConnectionRetryCount)
{
int SleepTime = _connectionRetryBackoffTimes[retryCount - 1] + (_connectionRetryRandomBackoff.Next() % 500);
//Thread.Sleep(SleepTime);
await Task.Delay(SleepTime);
}
return retryCount;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是async要求调用方法为async,等等。虽然最终重构为完全异步,但这远远超出了当前的重构范围。
我遇到的问题是如何从同步代码调用该方法并获取结果。使用
retryCount = PrepareForRetry(retryCount, command, timeout).Result;
Run Code Online (Sandbox Code Playgroud)
由于 UI 线程正在调用该方法,因此会造成死锁。我发现我可以通过将我的更改Task.Delay为
await Task.Delay(SleepTime).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
但我不完全明白这是做什么的。我也尝试过使用调用该方法
retryCount = Task.Run(async () => { await PrepareForRetry(retryCount, command, timeout).Result; });
Run Code Online (Sandbox Code Playgroud)
但这有错误“'int'不包含'GetAwaiter'的定义”,并且我无法找出如何继续获得结果。使用 …
我有以下类型的表达式Task<IEnumerable<PendingApprovalUserChangeRequest>>,我需要将其转换为IEnumerable<PendingApprovalUserChangeRequest>. 我怎么做 ?
Task<IEnumerable<PendingApprovalUserChangeRequest>> pendingChangeRequest = service.GetPendingChangeRequest();
Run Code Online (Sandbox Code Playgroud) 我试图将一组参数传递给任务。所以我创建了一个结构并将其传递给我的任务,如下所示:
my_type_t parameters_set;
//... assigning values to parameters_set
xTaskCreate( vMyTask, "MyTask", STACK_SIZE, (void*)parameters_set, 2,
&xHandle);
Run Code Online (Sandbox Code Playgroud)
在我的任务中,我尝试执行以下操作来检索结构值:
my_type_t received_parameters = (my_type_t) pvParameters;
Run Code Online (Sandbox Code Playgroud)
任务创建行在编译时触发以下错误:“无法转换为指针类型”,结构检索行触发“请求转换为非标量类型”错误。我知道如果我使用指针而不是变量本身,它会编译,但我不能这样做,因为创建任务的函数将会终止,并且任务将引用一个不再存在的变量。最后我将使用全局结构变量。
但我真正想理解的是为什么任务接受 int 值(不是通过引用),而不接受 typedef 结构?例如,以下代码片段可以毫无问题地构建和运行:
//Inside the function that creates the task
int x = 0;
xTaskCreate( vMyTask, "MyTask", STACK_SIZE, (void*)x, 2,
&xHandle);
//Inside the Task
int received_parameter = (int) pvParameters;
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有界面:
public Interface Inter1
{
....
Task<(ITestBulk testBulk, bool resultBulk)> GetBulk(int par1, int par2, string par3);
}
Run Code Online (Sandbox Code Playgroud)
然后得到了一些方法:
class MyClass{
internal bool m_resultBulk;
internal async void NewBulk()
{
....
(ITestBulk, m_resultBulk) = await service.GetBulk(1, 2, "ggg");
}
}
Run Code Online (Sandbox Code Playgroud)
但m_resultBulk找不到。我做错了什么?
我目前刚刚开始学习 Apache Spark,并且有一些代码我不太明白为什么无法编译。它说我发送到 myRDD forEach 的任务不可序列化,但是我正在观看的教程也做了类似的事情。任何想法或线索将不胜感激。
public class Main {
public static void main(String[] args) {
Logger.getLogger("org.apache").setLevel(Level.WARN);
List<Integer> inputData = new ArrayList<>();
inputData.add(25);
SparkConf conf = new SparkConf().setAppName("startingSpark").setMaster("local[*]");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<Integer> myRDD = sc.parallelize(inputData);
Integer result = myRDD.reduce((x, y) -> x + y);
myRDD.foreach( System.out::println );
System.out.println(result);
sc.close();
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
Exception in thread "main" org.apache.spark.SparkException: Task not serializable...
at com.virtualpairprogrammers.Main.main(Main.java:26)
Caused by: java.io.NotSerializableException: java.io.PrintStream
Serialization stack:
- object not serializable (class: java.io.PrintStream, value: java.io.PrintStream@11a82d0f)
- element …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Python程序,它同时运行从队列中获取的任务,以学习 asyncio.
通过与主线程(在 REPL 内)交互,项目将被放入队列中。每当一个任务被放入队列时,它应该立即被消耗并执行。我的方法是启动一个单独的线程并将队列传递到该线程内的事件循环。
这些任务正在运行,但只是按顺序运行,我不清楚如何同时运行这些任务。我的尝试如下:
import asyncio
import time
import queue
import threading
def do_it(task_queue):
'''Process tasks in the queue until the sentinel value is received'''
_sentinel = 'STOP'
def clock():
return time.strftime("%X")
async def process(name, total_time):
status = f'{clock()} {name}_{total_time}:'
print(status, 'START')
current_time = time.time()
end_time = current_time + total_time
while current_time < end_time:
print(status, 'processing...')
await asyncio.sleep(1)
current_time = time.time()
print(status, 'DONE.')
async def main():
while True:
item = task_queue.get()
if item == _sentinel:
break
await …Run Code Online (Sandbox Code Playgroud) task ×10
c# ×5
async-await ×4
asynchronous ×2
.net ×1
ada ×1
apache-spark ×1
binding ×1
c ×1
concurrency ×1
freertos ×1
gulp ×1
gulp-watch ×1
interface ×1
java ×1
python ×1
queue ×1
tuples ×1
xamarin ×1