Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the HTTP requests for the Wasm worker wait for the initial run_code() or run_file() to finish #5958

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crazy-dancers-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gradio/wasm": minor
---

feat:Make the HTTP requests for the Wasm worker wait for the initial `run_code()` or `run_file()` to finish
26 changes: 26 additions & 0 deletions js/wasm/src/promise-delegate.ts
Copy link
Member Author

@whitphx whitphx Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type PromiseImplFn<T> = ConstructorParameters<typeof Promise<T>>[0];

export class PromiseDelegate<T> {
private promiseInternal: Promise<T>;
private resolveInternal!: Parameters<PromiseImplFn<T>>[0];
private rejectInternal!: Parameters<PromiseImplFn<T>>[1];

constructor() {
this.promiseInternal = new Promise((resolve, reject) => {
this.resolveInternal = resolve;
this.rejectInternal = reject;
});
}

get promise(): Promise<T> {
return this.promiseInternal;
}

public resolve(value: T): void {
this.resolveInternal(value);
}

public reject(reason: unknown): void {
this.rejectInternal(reason);
}
}
11 changes: 11 additions & 0 deletions js/wasm/src/worker-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ReplyMessage
} from "./message-types";
import { MessagePortWebSocket } from "./messageportwebsocket";
import { PromiseDelegate } from "./promise-delegate";

export interface WorkerProxyOptions {
gradioWheelUrl: string;
Expand All @@ -20,6 +21,8 @@ export interface WorkerProxyOptions {
export class WorkerProxy {
private worker: globalThis.Worker;

private firstRunPromiseDelegate = new PromiseDelegate<void>();

constructor(options: WorkerProxyOptions) {
console.debug("WorkerProxy.constructor(): Create a new worker.");
// Loading a worker here relies on Vite's support for WebWorkers (https://vitejs.dev/guide/features.html#web-workers),
Expand Down Expand Up @@ -49,6 +52,7 @@ export class WorkerProxy {
code
}
});
this.firstRunPromiseDelegate.resolve();
}

public async runPythonFile(path: string): Promise<void> {
Expand All @@ -58,6 +62,7 @@ export class WorkerProxy {
path
}
});
this.firstRunPromiseDelegate.resolve();
}

// A wrapper for this.worker.postMessage(). Unlike that function, which
Expand All @@ -84,6 +89,12 @@ export class WorkerProxy {
}

public async httpRequest(request: HttpRequest): Promise<HttpResponse> {
// Wait for the first run to be done
// to avoid the "Gradio app has not been launched." error
// in case running the code takes long time.
// Ref: https://github.com/gradio-app/gradio/issues/5957
await this.firstRunPromiseDelegate.promise;

console.debug("WorkerProxy.httpRequest()", request);
const result = await this.postMessageAsync({
type: "http-request",
Expand Down
Loading